Part 1
In this puzzle, we have a long string consisting of multiple characters of lowercase, uppercase and symbol characters. Non-symbols characters are considered not corrupted and we should count the number of non corrupted characters in out input.We used the map function for transform every character in our input into a truth value. The truth values are summed up, since Python will quietly transform True to 1 and False to 0.
sum(map(only_uncorrupted, input))
The function for checking if a character is corrupted is taking the built-in functions for checking for lower- and uppercase:
def only_uncorrupted(c):
return c.islower() or c.isupper()
Part 2
In part 2, each non-corrupted character is given a value by its position in the alphabet. Lowercase character start with 1 for a until 26 for z, while uppercase character are in the range 27 - 52. We should sum up the values for the non-corrupted characters only. Corrupted characters we should ignore.
We again use the map function, but with a function for the character values instead. The values we get with the help of the ord function, which gets us the ASCII value of a character. We then take the ASCII value of a and A to get the position of the character in the alphabet:
def char_values(c):
if c.islower():
return ord(c) - ord("a") + 1
if c.isupper():
return ord(c) - ord("A") + 27
return 0
Part 3
In part 3, we now get a way of calculating the character value of symbol characters. The formula depends on the character value of the previous character. We should again sum up the character values for everything in our input.
Since we now depend on the previous value, we instead keep a list of all character values until now. In case of lower and upper characters, we use the previous method and append the value to our list. For symbols, we use the last value of the list and apply the formula. At the end we sum up our list for the answer.
No comments:
Post a Comment