IQ Test
Introduction
Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given numbers differs from the others
. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness, and return a position of this number.
! Keep in mind that your task is to help Bob solve a real IQ test
, which means indexes of the elements start from 1 (not 0)
Examples :
iq_test("2 4 7 8 10") => 3 // Third number is odd, while the rest of the numbers are even
iq_test("1 2 1 1") => 2 // Second number is even, while the rest of the numbers are odd
My submit
def iq_test(numbers):
ns = [int(x) for x in numbers.split(' ')]
odd_count = 0
even_count = 0
for i in range(0,3):
if ns[i] %2 == 0:
even_count += 1
else:
odd_count += 1
standard = int(odd_count > even_count)
index = 0
for i in range(0, len(ns)):
if ns[i] % 2 is not standard:
index = i + 1
return index
Best Practices
def iq_test(numbers):
e = [int(i) % 2 == 0 for i in numbers.split()]
return e.index(True) + 1 if e.count(True) == 1 else e.index(False) + 1
Summary
处理入参的时候就将其处理为True(双数),False(单数)的 [Bool],
灵活的使用的 list method,但是使用count可能会有性能问题
Replace With Alphabet Position
Introduction
Welcome. In this kata you are required to, given a string, replace every letter with its position in the alphabet. If anything in the text isn't a letter, ignore it and don't return it. a being 1, b being 2, etc. As an example:
alphabet_position("The sunset sets at twelve o' clock.")
Should return "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"
(As a string.)
My Submit
def alphabet_position(text):
letters = []
for s in text:
c = ord(s)
if c >= 65 and c <= 90:
c -= 64
elif c >= 97 and c <= 122:
c -= 96
else:
continue
letters.append(str(c))
return ' '.join(letters)
Best Practices
def alphabet_position(text):
return ' '.join(str(ord(c) - 96) for c in text.lower() if c.isalpha())
Summary
审错题目为了保留非字母字符,牺牲了比较便利的写法,距离最佳答案只差一步;
lower()使用的很巧妙
Count the number of Duplicates
Introduction
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.
Example
"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (bandB)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice
My Submit
def duplicate_count(text):
letters = [c for c in text.lower()]
exists = []
count = 0
for c in letters:
if c not in exists and letters.count(c) > 1:
count += 1
exists.append(c)
return count
Best Practices
def duplicate_count(s):
return len([c for c in set(s.lower()) if s.lower().count(c)>1])
Summary
看起来很简洁,实际并不是一个很好的答案,可跟推荐一个点赞不是特别高的方案
def duplicate_count(text):
seen = set()
dupes = set()
for char in text:
char = char.lower()
if char in seen:
dupes.add(char)
seen.add(char)
return len(dupes)
网友评论