Compare commits
No commits in common. "1a3284cb5588b2a334ed894ed5bd6f9cee3c3b21" and "1d684b4d657168c65c354fb4abba45bd20db1973" have entirely different histories.
1a3284cb55
...
1d684b4d65
@ -15,7 +15,7 @@ Repository for CDS-2020 Programming and Promt Engineering II
|
|||||||
|build|Build-System, Dependencies|build: update requirements.txt|
|
|build|Build-System, Dependencies|build: update requirements.txt|
|
||||||
|
|
||||||
# Codewars
|
# Codewars
|
||||||
|Title|Source (src/codewars/)|Test (test/test_codewars/)|URL|
|
|Title|Source (src/codewars/)|Test (test/codewars/)|URL|
|
||||||
|-|-|-|-|
|
|-|-|-|-|
|
||||||
|Find the force of gravity between two objects|kata_force_of_gravity.py|test_force_of_gravity.py|[5b609ebc8f47bd595e000627](https://www.codewars.com/kata/5b609ebc8f47bd595e000627)|
|
|Find the force of gravity between two objects|kata_force_of_gravity.py|test_force_of_gravity.py|[5b609ebc8f47bd595e000627](https://www.codewars.com/kata/5b609ebc8f47bd595e000627)|
|
||||||
|The Lamp: Revisited|kata_the_lamp.py|test_the_lamp.py|[570e6e32de4dc8a8340016dd](https://www.codewars.com/kata/570e6e32de4dc8a8340016dd)|
|
|The Lamp: Revisited|kata_the_lamp.py|test_the_lamp.py|[570e6e32de4dc8a8340016dd](https://www.codewars.com/kata/570e6e32de4dc8a8340016dd)|
|
||||||
@ -30,4 +30,3 @@ Repository for CDS-2020 Programming and Promt Engineering II
|
|||||||
|Who has the most money?|kata_who_the_most_money.py|test_who_the_most_money.py|[528d36d7cc451cd7e4000339](https://www.codewars.com/kata/528d36d7cc451cd7e4000339)|
|
|Who has the most money?|kata_who_the_most_money.py|test_who_the_most_money.py|[528d36d7cc451cd7e4000339](https://www.codewars.com/kata/528d36d7cc451cd7e4000339)|
|
||||||
|Next bigger number with the same digits|kata_next_bigger_number_same_digits.py|test_next_bigger_number_same_digits.py|[55983863da40caa2c900004e](https://www.codewars.com/kata/55983863da40caa2c900004e)|
|
|Next bigger number with the same digits|kata_next_bigger_number_same_digits.py|test_next_bigger_number_same_digits.py|[55983863da40caa2c900004e](https://www.codewars.com/kata/55983863da40caa2c900004e)|
|
||||||
|Snail|kata_snail.py|test_snail.py|[521c2db8ddc89b9b7a0000c1](https://www.codewars.com/kata/521c2db8ddc89b9b7a0000c1)|
|
|Snail|kata_snail.py|test_snail.py|[521c2db8ddc89b9b7a0000c1](https://www.codewars.com/kata/521c2db8ddc89b9b7a0000c1)|
|
||||||
|Chinese Numeral Encoder|kata_chinese_numeral_encoder.py|test_chinese_numeral_encoder.py|[52608f5345d4a19bed000b31](https://www.codewars.com/kata/52608f5345d4a19bed000b31)|
|
|
||||||
|
|||||||
@ -1,109 +0,0 @@
|
|||||||
numerals = {
|
|
||||||
"-": "负",
|
|
||||||
".": "点",
|
|
||||||
0: "零",
|
|
||||||
1: "一",
|
|
||||||
2: "二",
|
|
||||||
3: "三",
|
|
||||||
4: "四",
|
|
||||||
5: "五",
|
|
||||||
6: "六",
|
|
||||||
7: "七",
|
|
||||||
8: "八",
|
|
||||||
9: "九",
|
|
||||||
10: "十",
|
|
||||||
100: "百",
|
|
||||||
1000: "千",
|
|
||||||
10000: "万",
|
|
||||||
}
|
|
||||||
|
|
||||||
# "Ten^x" dictonary
|
|
||||||
T = {1: 0, 2: 10, 3: 100, 4: 1000, 5: 10000}
|
|
||||||
|
|
||||||
|
|
||||||
def to_chinese_numeral(n):
|
|
||||||
# Check if negative, turn positive and remeber state
|
|
||||||
if n < 0:
|
|
||||||
n = abs(n)
|
|
||||||
is_negative = True
|
|
||||||
else:
|
|
||||||
is_negative = False
|
|
||||||
|
|
||||||
# return numerals[n] if n in numerals dictonary
|
|
||||||
if n % 100 != 0:
|
|
||||||
try:
|
|
||||||
if is_negative:
|
|
||||||
return "".join([numerals["-"], numerals[n]])
|
|
||||||
else:
|
|
||||||
return numerals[n]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Split number in iteger list and fractional (None if not exists)
|
|
||||||
if type(n) is not int:
|
|
||||||
n = str(n).split(".")
|
|
||||||
integer = [int(x) for x in n[0]]
|
|
||||||
fractional = [int(x) for x in n[1]]
|
|
||||||
else:
|
|
||||||
integer = [int(x) for x in str(n)]
|
|
||||||
fractional = None
|
|
||||||
|
|
||||||
# Blueprint list will contain building instruction on how to encode the number: 90090 -> [9, 10000, 0, 9, 10]
|
|
||||||
blueprint = []
|
|
||||||
|
|
||||||
blueprint.append(integer[0]) # First digit won't be 0 it can just be appended
|
|
||||||
blueprint.append(T[len(integer)]) # Length of integer determines power of ten
|
|
||||||
integer.pop(0) # Pop first digit
|
|
||||||
|
|
||||||
# create rest of blueprint by looping rest of integer
|
|
||||||
for _ in range(0, len(integer)):
|
|
||||||
# 0 needs no power of ten
|
|
||||||
if integer[0] == 0:
|
|
||||||
blueprint.append(0)
|
|
||||||
else:
|
|
||||||
blueprint.append(integer[0])
|
|
||||||
blueprint.append(T[len(integer)])
|
|
||||||
integer.pop(0)
|
|
||||||
|
|
||||||
# Pop last digit in blueprint if it's a 0
|
|
||||||
for i in range(len(blueprint) - 1, 0, -1):
|
|
||||||
if blueprint[i] == 0:
|
|
||||||
blueprint.pop(i)
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
||||||
# Remove grouped zeros
|
|
||||||
was_zero = False
|
|
||||||
index_to_remove = []
|
|
||||||
for j, i in enumerate(blueprint):
|
|
||||||
if i == 0 and not was_zero:
|
|
||||||
was_zero = True
|
|
||||||
elif i == 0 and was_zero:
|
|
||||||
index_to_remove.append(j)
|
|
||||||
elif i != 0 and was_zero:
|
|
||||||
was_zero = False
|
|
||||||
|
|
||||||
index_to_remove.sort(
|
|
||||||
reverse=True
|
|
||||||
) # Reverse indices to avoid index errors when applying pop()
|
|
||||||
[blueprint.pop(x) for x in index_to_remove]
|
|
||||||
|
|
||||||
# remove first item in blueprint if second item in blueprint is between 10 and 19. Skip step if index error
|
|
||||||
try:
|
|
||||||
if sum(blueprint[0:2]) == 11:
|
|
||||||
blueprint.pop(0)
|
|
||||||
except IndexError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Add symbols - if n was negative and . if it was a fraction
|
|
||||||
if is_negative:
|
|
||||||
blueprint.insert(0, "-")
|
|
||||||
|
|
||||||
if fractional is not None:
|
|
||||||
blueprint.append(".")
|
|
||||||
[blueprint.append(x) for x in fractional]
|
|
||||||
|
|
||||||
# Apply chinese encoding to blueprint and fractional
|
|
||||||
encoding = [numerals[x] for x in blueprint]
|
|
||||||
|
|
||||||
return "".join(encoding)
|
|
||||||
@ -1,15 +0,0 @@
|
|||||||
from src.codewars.kata_chinese_numeral_encoder import to_chinese_numeral
|
|
||||||
|
|
||||||
|
|
||||||
def test_to_chinese():
|
|
||||||
assert to_chinese_numeral(9) == "九"
|
|
||||||
assert to_chinese_numeral(-5) == "负五"
|
|
||||||
assert to_chinese_numeral(0.5) == "零点五"
|
|
||||||
assert to_chinese_numeral(10) == "十"
|
|
||||||
assert to_chinese_numeral(110) == "一百一十"
|
|
||||||
assert to_chinese_numeral(111) == "一百一十一"
|
|
||||||
assert to_chinese_numeral(1000) == "一千"
|
|
||||||
assert to_chinese_numeral(10000) == "一万"
|
|
||||||
assert to_chinese_numeral(10006) == "一万零六"
|
|
||||||
assert to_chinese_numeral(10306.005) == "一万零三百零六点零零五"
|
|
||||||
assert to_chinese_numeral(-10.000001) == "负十点零零零零零一"
|
|
||||||
Loading…
x
Reference in New Issue
Block a user