Multiples of 3 or 5
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.
Note: If the number is a multiple of both 3 and 5, only count it once. Also, if a number is negative, return 0(for languages that do have them)
def solution(number):
threes = range(3, number, 3)
fives = range(5, number, 5)
return sum(list(set(threes + fives)))
def solution(number):
#yurong
li = list(range(number))
a = [i for i in li if i%3 == 0 or i%5 == 0]
return sum(a)
Sum of Digits / Digital Root 递归/while循环
Examples
16 --> 1 + 6 = 7
942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6
132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6
493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11
def digital_root(n):
while n>9:
n=sum(map(int,str(n)))
return n
def digital_root(n):
return n if n < 10 else digital_root(sum(map(int,str(n))))
def digital_root(n):
#yurong learning from csdn
a = [int(i) for i in str(n)] # a=list(map(int,str(n)))
if sum(a) >=10:
return digital_root(sum(a))
else:
return sum(a)
def dig(num):
#steven:一定要有两个return
if num // 10 ==0:
return num
li = [int(x) for x in str(num)]
return dig(sum(li))
Stop gninnipS My sdroW!
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (like the name of this kata).
Strings passed in will consist of only letters and spaces.
Spaces will be included only when more than one word is present.
Examples:
spinWords("Hey fellow warriors") => "Hey wollef sroirraw"
spinWords("This is a test") => "This is a test"
spinWords("This is another test") => "This is rehtona test"
def spin_words(sentence):
#yurong
#三目运算,对每一个字符串遍历。符合if的走第一句话,不符合的走else
return " ".join(i[::-1] if len(i)>=5 else i for i in sentence.split(" "))
Who likes it? 点赞 条件控制/字典
You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.
Implement a function likes :: [String] -> String, which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples:
likes([]) # must be "no one likes this"
likes(["Peter"]) # must be "Peter likes this"
likes(["Jacob", "Alex"]) # must be "Jacob and Alex like this"
likes(["Max", "John", "Mark"]) # must be "Max, John and Mark like this"
likes(["Alex", "Jacob", "Mark", "Max"]) # must be "Alex, Jacob and 2 others like this"
def likes(names):
#py没有case when条件控制,因为有字典
formats = {
0: "no one likes this",
1: "{} likes this",
2: "{} and {} like this",
3: "{}, {} and {} like this",
4: "{}, {} and {others} others like this"
}
n = len(names)
return formats[min(n,4)].format(*names, others=n-2)
def likes(names):
#yurong
if len(names) == 0:
return "no one likes this"
elif len(names) == 1:
return f"{names[0]} likes this"
elif len(names) == 2:
return f"{names[0]} and {names[1]} like this"
elif len(names) == 3:
return f"{names[0]}, {names[1]} and {names[2]} like this"
else :
return f"{names[0]}, {names[1]} and {len(names)-2} others like this"
Persistent Bugger.
返回计算的次数
Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.
For example:
persistence(39) # returns 3, because 39=27, 27=14, 1*4=4
# and 4 has only one digit
persistence(999) # returns 4, because 999=729, 729=126,
# 126=12, and finally 1*2=2
persistence(4) # returns 0, because 4 is already a one-digit
def persistence(n):
#yurong
count = 0
while n >= 10:
count+=1
n = multi(n)
return count
def multi(j):
a = [int(i) for i in str(j)]
x = 1
for i in range(len(a)):
x = x*a[i]
return x
Counting Duplicates
Count the number of Duplicates
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 (b and B)
"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
def duplicate_count(text):
# Yurong
tex = text.lower()
a = [i for i in tex if tex.count(i)>=2 ]
return len(set(a))
Duplicate Encoder
The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.
Examples
"din" => "((("
"recede" => "()()()"
"Success" => ")())())"
"(( @" => "))(("
def duplicate_encode(word):
#yurong
wor = word.lower()
a = [i for i in wor]
return "".join(r"(" if a.count(i)==1 else r")" for i in a)
Array.diff
test.assert_equals(array_diff([1,2], [1]), [2], "a was [1,2], b was [1], expected [2]")
test.assert_equals(array_diff([1,2,2], [1]), [2,2], "a was [1,2,2], b was [1], expected [2,2]")
test.assert_equals(array_diff([1,2,2], [2]), [1], "a was [1,2,2], b was [2], expected [1]")
test.assert_equals(array_diff([1,2,2], []), [1,2,2], "a was [1,2,2], b was [], expected [1,2,2]")
test.assert_equals(array_diff([], [1,2]), [], "a was [], b was [1,2], expected []")
test.assert_equals(array_diff([1,2,3], [1, 2]), [3], "a was [1,2,3], b was [1, 2], expected [3]")
def array_diff(a, b):
c = [i for i in a if i not in b]
return c
def array_diff(a, b):
#yurong
if len(b) == 0:
return a
else:
for i in range(len(b)):
while b[i] in a:
a.remove(b[i])
return a