-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcode-samples.py
More file actions
67 lines (57 loc) · 2.03 KB
/
Copy pathcode-samples.py
File metadata and controls
67 lines (57 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# task
def is_happy(s):
"""You are given a string s.
Your task is to check if the string is happy or not.
A string is happy if its length is at least 3 and every 3 consecutive letters are distinct
For example:
is_happy(a) => False
is_happy(aa) => False
is_happy(abcd) => True
is_happy(aabb) => False
is_happy(adb) => True
is_happy(xyy) => False
"""
# test
def check(candidate):
# Check some simple cases
assert candidate("a") == False, "a"
assert candidate("aa") == False, "aa"
assert candidate("abcd") == True, "abcd"
assert candidate("aabb") == False, "aabb"
assert candidate("adb") == True, "adb"
assert candidate("xyy") == False, "xyy"
assert candidate("iopaxpoi") == True, "iopaxpoi"
assert candidate("iopaxioi") == False, "iopaxioi"
# pred
def is_happy(s):
"""You are given a string s.
Your task is to check if the string is happy or not.
A string is happy if its length is at least 3 and every 3 consecutive letters are distinct
For example:
is_happy(a) => False
is_happy(aa) => False
is_happy(abcd) => True
is_happy(aabb) => False
is_happy(adb) => True
is_happy(xyy) => False
"""
if len(s) < 3:
return False
for i in range(len(s) - 2):
if s[i] == s[i + 1] or s[i] == s[i + 2] or s[i + 1] == s[i + 2]:
return False
return True
# my task
def debt_repayment_time(income, debt, annual_interest_rate) -> None:
"""You are given three numbers: income, debt, and annual interest rate on the debt.
Your task is to calculate how long it will take to pay off the debt given that you pay 10% of your income each month.
For example:
debt_repayment_time(5000, 1000, 5) => 11
debt_repayment_time(5000, 1000, 5) => 0
debt_repayment_time(20000, 50000, 10) => 100
"""
def check_debt_repayment_time(candidate):
assert candidate(5000, 10000, 10) == 22
assert candidate(10000, 20000, 5) == 21
assert candidate(15000, 50000, 1) == 33
assert candidate(15000, 50000, 10) == 40