© getcodify.com |
由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址
Notation: Those Challenges come from Rosalind
Introduction
Function:
def run(a, b):
Result = a + b
print("hello function")
return Result
Function test:
C = run(1 , 5)
print(C)
hello function
6
Let the fun begin!
Calculate
Link
Given: Two positive integers a and b, each less than 1000.
Return: The integer corresponding to the square of the hypotenuse of the right triangle whose legs have lengths a and b.
in: 3 5
out: 34
def run(a, b):
Result = (a+b)**2 - 2ab
print(Result)
run(3, 4)
String splice
Link
Given: A string s of length at most 200 letters and four integers a, b, c and d.
Return: The slice of this string from indices a through b and c through d (with space in between), inclusively. In other words, we should include elements s[b] and s[d] in our slice.
in:
HumptyDumptysatonawallHumptyDumptyhadagreatfallAlltheKingshorsesandalltheKingsmenCouldntputHumptyDumptyinhisplaceagain.
22 27 97 102
out:
Humpty Dumpty
def run(Str, a, b, c, d):
Result = Str[a:b+1] + " " Str[c:d+1]
print(Result)
Str = "HumptyDumptysatonawallHumptyDumptyhadagreatfallAlltheKingshorsesandalltheKingsmenCouldntputHumptyDumptyinhisplaceagain."
a = 22; b = 27; c = 97; d = 102
run(Str, a, b, c, d)
loop
Link
Given: Two positive integers a and b (a
Return: The sum of all odd integers from a through b, inclusively.
in:
100 200
out:
7500
def run(a, b):
List = [a, b]
List.sort()
Result = 0
for i in range(List[0], List[1]+1):
if i % 2 == 1:
Result += i
print(Result)
run(100, 200)
Reading and writing
Link
Given: A file containing at most 1000 lines.
Return: A file containing all the even-numbered lines from the original file. Assume 1-based numbering of lines.
def run(INPUT, OUTPUT):
In = open(INPUT, 'r').readlines()
Num = 0
Result = ""
for i in In:
if Num % 2 == 1:
Result += i
Num += 1
print(Result)
F = open(OUTPUT, 'w')
F.write(Result)
F.close()
Words count
Link
Given: A string s of length at most 10000 letters.
Return: The number of occurrences of each word in s, where words are separated by spaces. Words are case-sensitive, and the lines in the output can be in any order.
In:
We tried list and we tried dicts also we tried Zen
Out:
and 1
We 1
tried 3
dicts 1
list 1
we 2
also 1
Zen 1
def run(Str):
List = Str.split(" ")
Index = list(set(List))
Result = ""
for i in Index:
if i != "":
Result += i +" " + str(List.count(i)) +"\n"
print(Result)
Enjoy~
由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址
GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗