Skip to content

Solutions

tiago edited this page Nov 13, 2019 · 4 revisions

1 - Hello!

# Read inputs
name = input()
age = int(input)

# Print formatted string
print("Hello", name)
print("Next year you'll be", age + 1)

2 - Odd or Even?

num = int(input())

# If the remainder of a division by 2 is 0, the number is even
if (num % 2 == 0):
    print("Even")
else:
    print("Odd")

3 - The string's classic

names = input().split()

name = ""
if len(names) == 1:  # If there is only one word, our solution is done
    name = names[0]
else:  # There were two or more words
    name = names[0]

    #  For every word in names, from index 1 to the penultimate one
    for i in range(1, len(names) - 1):  # If len(names) is 2, this loop won't run
        if names[i].capitalize() == names[i]:  # If the word is capitalized, to ignore "da", "de", etc
            name += " " + names[i][0] + "."  # Add the first letter (already capitalized)

    name += " " + names[-1] # Add the last name

print(name)

4 - Case converter

#  Function with 2 arguments, the full string and the separator
def convert_camel_case(string, sep):
    res = ""
    for char in string:
        if char.isupper():  # If it's an uppercase, add it lowered with the sep
            res += sep + char.lower()
        else:
            res += char  # Else add the same char
    return res

string = input()
sep = input()
print("Function:", convert_camel_case(string, sep))  # Call the function and print its return value
print("Generator:", "".join([(sep + letter.lower()) if letter.isupper() else letter for letter in string]))

5 - Octal

num = int(input)
num2 = num  # Preserve the num for the verification

if (num == 0):  # If num is 0, it wouldn't enter the loop
    res = "0"
else:
    res = ""  # Create an empty string to construct our solution over it
    while num > 0:  # While there are digits to process
        res += str(int(num % 8))  # 12 % 8 = 4, add "4" to our result
        num = int(num / 8)  # 12 / 8 = 1, set our next num to 1

    res = res[::-1] # As we built our solution backwards, reverse the string

print("Result: 0o", res, sep="")
print("Confirmation:", oct(num2)) # Use this to confirm your results

6 - Anagrams

# create a set from a given string's characters
# (after converting them to lower-case if necessary)
given_word = set(input().lower())

# for each word in the given sentence
for word in input().split():
  if given_word == set(word.lower()):
    print(word)

7 - Sort and comparison functions

# a)
# a)
num = int(input())

alist = []
for i in range(num):
    read = input().split()
    #  Appends to the list a tuple with the word and the float: (Joao, 21.3)
    alist.append( (read[0], float(read[1])) )

print("a)", alist[-1])

# b)
def sorting_key(t):  # Receives an element and return something for the function to return
    return t[1]

# Save the list sorted using our sorting_key() and in reverse order (descending)
sorted_list = sorted(alist, key=sorting_key, reverse=True)

print("b)", sorted_list[-1])