Решение на doge от Георги Янев

Обратно към всички решения

Към профила на Георги Янев

Резултати

  • 10 точки от тестове
  • 1 отнета точка
  • 9 точки общо
  • 15 успешни тест(а)
  • 0 неуспешни тест(а)

Код

def wow_such_much(start, end):
"""
Take start and end integer arguments and generate a list
containing the arithmetic progression. Return a list of
those integers converted to strings and if the number is
divided by 3 switch replace it with such, if divided by 5
with much and if divided by 15 with suchmuch
"""
divisors = [(3, 'such'), (5, 'much')]
def compute_such_much(number, ldivisors):
new_value = ''
for i in ldivisors:
if number % i[0] == 0 and number != 0:
new_value += i[1]
return new_value if new_value else str(number)
changed_list = [compute_such_much(i, divisors) for i in
range(start, end)]
return changed_list
def count_doge_words(sentence):
"""Take a sentence as argument and return a positive or
equal to zero integer which represent how many times
specified words are found in it
"""
search_words = ['wow', 'lol', 'so', 'such', 'much', 'very']
counter = 0
for w in sentence.split(' '):
if w in search_words:
counter += 1
return counter

Лог от изпълнението

...............
----------------------------------------------------------------------
Ran 15 tests in 0.010s

OK

История (2 версии и 2 коментара)

Георги обнови решението на 07.03.2014 01:33 (преди над 10 години)

+def wow_such_much(start, end):
+ """
+ Take start and end integer arguments and generate a list
+ containing the arithmetic progression. Return the list of
+ those integers but if the number is divided by 3 switch the
+ value with such if divided by 5 with much and if divided by
+ 15 with suchmuch
+ """
+ provided_list = range(start, end)
+ divisors = [(3, 'such'), (5, 'much'), (3*5, 'suchmuch')]
+ changed_list = []
+
+ for i in provided_list:
+ new_value = i
+ if i == 0:
+ changed_list.append(new_value)
+ continue
+ for d in divisors:
+ if (i / d[0]).is_integer():
+ new_value = d[1]
+ else:
+ pass
+ changed_list.append(new_value)
+ return changed_list
+
+def count_doge_words(sentence):
+ """Take a sentence as argument and return a positive or
+ equal to zero integer which represent how many times
+ specified words are found in it
+ """
+ search_words = ['wow', 'lol', 'so', 'such', 'much', 'very']
+ counter = 0
+
+ for w in sentence.split(' '):
+ if w in search_words:
+ counter += 1
+ return counter

Погледни пак условието

  • Числата във върнатия списък трябва също да са конвертирани до низове.
  • Погледни какво прави операторът % в python.
  • Помисли дали тези вложени if-ове и for цикли могат да се заменят с по-проста конструкция

Георги обнови решението на 09.03.2014 02:09 (преди над 10 години)

def wow_such_much(start, end):
"""
Take start and end integer arguments and generate a list
- containing the arithmetic progression. Return the list of
- those integers but if the number is divided by 3 switch the
- value with such if divided by 5 with much and if divided by
- 15 with suchmuch
+ containing the arithmetic progression. Return a list of
+ those integers converted to strings and if the number is
+ divided by 3 switch replace it with such, if divided by 5
+ with much and if divided by 15 with suchmuch
"""
- provided_list = range(start, end)
- divisors = [(3, 'such'), (5, 'much'), (3*5, 'suchmuch')]
- changed_list = []
+ divisors = [(3, 'such'), (5, 'much')]
- for i in provided_list:
- new_value = i
- if i == 0:
- changed_list.append(new_value)
- continue
- for d in divisors:
- if (i / d[0]).is_integer():
- new_value = d[1]
- else:
- pass
- changed_list.append(new_value)
+ def compute_such_much(number, ldivisors):
+ new_value = ''
+ for i in ldivisors:
+ if number % i[0] == 0 and number != 0:
+ new_value += i[1]
+ return new_value if new_value else str(number)
+
+ changed_list = [compute_such_much(i, divisors) for i in
+ range(start, end)]
return changed_list
def count_doge_words(sentence):
"""Take a sentence as argument and return a positive or
equal to zero integer which represent how many times
specified words are found in it
"""
search_words = ['wow', 'lol', 'so', 'such', 'much', 'very']
counter = 0
for w in sentence.split(' '):
if w in search_words:
counter += 1
return counter

втора версия на кода

Не съм сигурен дали вътрешната функция да ползва променливата на външната директно или да й бъде подадена както реално съм направил, но вторият начин ми се стори по удачен.