Created
February 9, 2019 13:04
-
-
Save inesusvet/eed76c5a1c29b02fe48f812268fb8a59 to your computer and use it in GitHub Desktop.
Coding dojo in Minsk session result for 2019-02-09
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
See https://www.codewars.com/kata/love-vs-friendship/train/python | |
If a = 1, b = 2, c = 3 ... z = 26 | |
Then l + o + v + e = 54 | |
and f + r + i + e + n + d + s + h + i + p = 108 | |
So friendship is twice stronger than love :-) | |
The input will always be in lowercase and never be empty. | |
""" | |
import string | |
def words_to_marks(word): | |
""" | |
>>> words_to_marks('attitude') | |
100 | |
>>> words_to_marks('friends') | |
75 | |
>>> words_to_marks('family') | |
66 | |
>>> words_to_marks('selfness') | |
99 | |
>>> words_to_marks('knowledge') | |
96 | |
""" | |
return sum( | |
word.count(char) * (i + 1) | |
for i, char in enumerate(string.ascii_lowercase) | |
) | |
return sum(string.ascii_lowercase.index(c) + 1 for c in word) | |
total = 0 | |
for char in word: | |
i = string.ascii_lowercase.index(char) + 1 | |
total += i | |
return total |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
See https://www.codewars.com/kata/number-of-people-in-the-bus | |
There is a bus moving in the city, and it takes and drop some people in each bus stop. | |
You are provided with a list (or array) of integer arrays (or tuples). Each integer array has two items which represent number of people get into bus (The first item) and number of people get off the bus (The second item) in a bus stop. | |
Your task is to return number of people who are still in the bus after the last bus station (after the last array). Even though it is the last bus stop, the bus is not empty and some people are still in the bus, and they are probably sleeping there :D | |
Take a look on the test cases. | |
Please keep in mind that the test cases ensure that the number of people in the bus is always >= 0. So the return integer can't be negative. | |
The second value in the first integer array is 0, since the bus is empty in the first bus stop. | |
""" | |
def number(bus_stops): | |
""" | |
>>> number([[10,0],[3,5],[5,8]]) | |
5 | |
>>> number([[3,0],[9,1],[4,10],[12,2],[6,1],[7,10]]) | |
17 | |
>>> number([[3,0],[9,1],[4,8],[12,2],[6,1],[7,8]]) | |
21 | |
>>> number([[0,0]]) | |
0 | |
""" | |
return sum(i - o for i, o in bus_stops) | |
return reduce( | |
lambda accum, (i, o): | |
accum + i - o, | |
bus_stops, | |
0, | |
) | |
total = 0 | |
for i, o in bus_stops: | |
total += i - o | |
return total | |
total = 0 | |
for j in range(len(bus_stops)): | |
stop = bus_stops[j] | |
i = stop[0] | |
o = stop[1] | |
total += i | |
total -= o | |
return total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment