1

I'm busy with a software engineering bootcamp. There is a question which I am struggling to answer, especially since it specifically requires me to not make use of functions. This is the question:

Declare a variable called hero equals to Super Man
Print it out in the following way:

S^U^P^E^R M^A^N

Do Not use any functions to do this

normally I could easily do this with the use of functions, but right now I am completely stuck

cs95
  • 379,657
  • 97
  • 704
  • 746
Zee Dhlomo
  • 69
  • 1
  • 1
  • 9

7 Answers7

2

there might be a shorter way with nested list comprehension but this could work:

lower_to_upper = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L', 'm': 'M', 'n': 'N', 'o': 'O', 'p': 'P', 'q': 'Q', 'r': 'R', 's': 'S', 't': 'T', 'u': 'U', 'v': 'V', 'w': 'W', 'x': 'X', 'y': 'Y', 'z': 'Z'}
hero = "Super Man"
string_list = ['^'+lower_to_upper[c] if c in lower_to_upper else '^'+c for c in hero ]
new_string = ""
for c in string_list:
    if c[1] == " ":
        c = " "
    new_string += c
new_string = new_string[1:]
print new_string

of course this does use internal python function like __iter__ , __next__ , __getattr__ , __getitem__ , etc... but if you try to go down that rabbit hole you will find you actually cannot write any python code without calling at least some functions either implicitly or explicitly

AntiMatterDynamite
  • 1,495
  • 7
  • 17
1

If you consider methods != functions which in some respects are you can have:

hero = 'Super Man'
print(' '.join(['^'.join([s.upper() for s in word]) for word in hero.split()]))

which prints:

S^U^P^E^R M^A^N

.join(), .upper() and .split() are all string methods. Other than that, only a list-comprehension is used.

Ma0
  • 15,057
  • 4
  • 35
  • 65
1

You can consider using the ASCII values. The difference in the ASCII values of 'a' and 'A' can be added to the variable.

hero = "Super man"
hero1 = ""

for i in hero:
    if ord(i) in range(97,122):
        p = chr(ord(i) - 32) + "^"
        hero1 = hero1 + p
    elif ord(i) in range(65,90):
        hero1 = hero1 + i + "^"
    else:
        hero1 = hero1 + i
print hero1[:-1]
1

You can try this -

hero = 'Super Man'
# Add extra space at the end of main string to make each word in balance with
# word + ' ', so that last word can be manipulated easily.
hero += ' '
# declare new variable to hold manipulated string.
tmp = ''
i, length = 0, len(hero)
while i < length:
    ch = hero[i]
    # if current character is space then you need not to do anything just skip to next iteration.
    if ch == ' ':
        i += 1
        continue
    # if current character is in lowercase then make it in uppercase by using ASCII value
    # without using function,
    # ord() , this function gives ASCII integer value of character
    # chr() , this function gives character value for integer ASCII value
    if ch >= 'a' and ch <='z':
        ch = chr(ord(ch) - 32)

    # if next character is space then add character with space otherwise
    # add character with '^' symbol.
    if hero[i+1] == ' ':
        tmp += ch + ' '
    else:
        tmp += ch + '^'
    # increment the counter variable
    i += 1

print(tmp)

Output = S^U^P^E^R M^A^N

1

Assuming that you use an ASCII platform, or one of the numerous charsets using ASCII for plain letters (includes latin1 and utf8), it can be done by processing the code of individual chars. Uppercase letters A-Z have code in [0x41-0x5A] range, and lower case a-z in [0x61-0x7A] range. Ok for the conversion part...

But you need to add ^ only between letters and not between a letter and a non alphabet character, so you need to remember if previous character was an alphabet and look whether current one is too. Now we can start coding that in Python:

hero = "Super Man"
# take the code of any character in the string
l = [ ord(x) for x in hero ]
# convert lower case alphabet to upper case
l = [ x - 0x20 if (x >= 0x61 and x <= 0x7A) else x for x in l ]
# prepare a string for storing the result
resul = ""
caret = False
# process characters...
for i in l:
    if caret and i >= 0x41 and i <= 0x5A:
        resul += '^'
    resul += chr(i)
    caret = i >= 0x41 and i <= 0x5A
# print the result
print(resul)

Successfully outputs:

S^U^P^E^R M^A^N

But this still uses one function: ord to take the code of a letter. chr is not a function here but the way to create a character (type chr) for its integer code. In Python 3, you can use the bytes type to avoid usage of ord:

hero = "Super Man"
# take the code of any character in the string
l = bytes(hero)
...
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

Since they have given you task and you cann't use built-in methods I would say make a dict for given string and express your

solution as:-

here = "Super Man"
a = {'S':'S','u':'U','p':'P','e':'E','a':'A','n':'N','M':'M','r':'R'}
b = '^'.join([a.get(x) for x in here if a.get(x)])
print(b) #expected output

S^U^P^E^R^M^A^N

Narendra
  • 1,511
  • 1
  • 10
  • 20
1

With sting manipulation you can use .split() to split to two words and then .join() to join it with the special character. To avoid having 'magic strings' perhaps assign a variable to the special character.

hero = "Super Man".upper()
word = hero.split()
special_char = "^"

There can be 2 ways to display the output S^U^P^E^R M^A^N

Method 1: using join each word with the special character.

print(special_char.join(word[0])
      + " "
      + special_char.join(word[1]))

Method 2: assign variables on each word separately, and then .join() with the special character

word_1 = special_char.join(word[0])
word_2 = special_char.join(word[1])

print(word_1, word_2)
Nadia
  • 11
  • 1