Skip to content

Commit b24d5c3

Browse files
committed
better organization.
0 parents  commit b24d5c3

File tree

490 files changed

+118328
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

490 files changed

+118328
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This repository contains my personal solution to the
2+
7_kyu's most popular challenges of the site codewars.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def add_binary(a, b):
5+
6+
# This function sums two numbers (a, b),\
7+
# and return the result in binary as str.
8+
return f'{bin(a + b)[2:]}'
9+
# Bin converts decimal to binary.
10+
# return format(a + b, 'b') - clever.
11+
12+
print(add_binary(1, 1)) # Outputs - 10
13+
print(add_binary(5, 9)) # Outputs - 1110
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def arithmetic(a, b, operator):
5+
6+
# A simple calculator.
7+
if operator == 'add':
8+
return a + b
9+
elif operator == 'subtract':
10+
return a - b
11+
elif operator == 'multiply':
12+
return a * b
13+
elif operator == 'divide':
14+
return a / b
15+
16+
print(arithmetic(5, 2, 'add')) # Outputs - 7
17+
print(arithmetic(5, 2, 'subtract')) # Outputs - 3
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def calculate_years(principal, interest, tax, desired):
5+
6+
# This function calculates how many years\
7+
# is necessary to reach a desired amount of money.
8+
year = 0
9+
while principal < desired:
10+
principal += principal * interest - principal * interest * tax
11+
year += 1
12+
return year
13+
14+
print(calculate_years(1000, 0.05, 0.18, 1100))
15+
# Outputs - 3 years. It means,\ starting with a principal\
16+
# of 1000 it will take 3 years to reach a desired amount of 1100.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def capitalize(s):
5+
6+
# Given a string, capitalize the letters that occupy even indexes\
7+
# and odd indexes separately. Ex: ['AbCdEf', 'aBcDeF']
8+
a = ''.join([l.upper() if n % 2 else l for n, l in enumerate(s)])
9+
return [a, a.swapcase()]
10+
11+
print(capitalize('abracadabra')) # Outputs - ['AbRaCaDaBrA', 'aBrAcAdAbRa']
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
print('From Code Wars.')
2+
print()
3+
4+
def capitals(word):
5+
6+
# This function takes a word and return all\
7+
# indexes of capitalized letters.
8+
return [a[0] for a in enumerate(word) if a[1].isupper()]
9+
10+
print(capitals('CodEWaRs')) # Outputs - [0, 3, 4, 6]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from datetime import datetime
2+
print('From Code Wars.')
3+
print()
4+
5+
def check_coupon(entered_code, correct_code, current_date, expiration_date):
6+
7+
# This function checks if a coupon is valid or not.
8+
return entered_code is correct_code \
9+
and datetime.strptime(current_date, "%B %d, %Y") \
10+
<= datetime.strptime(expiration_date, "%B %d, %Y")
11+
12+
print(check_coupon('123', '123', 'September 5, 2014', 'October 1, 2014'))
13+
# Outputs - True
14+
print(check_coupon('123a', '123', 'September 5, 2014', 'October 1, 2014'))
15+
# Outputs - False
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
print('From Code Wars.')
2+
print()
3+
4+
def check_exam(arr1, arr2):
5+
6+
# This function checks an exam (arr1) with the student's answers (arr2),
7+
# and return the total points or 0 if the total is negative.
8+
return max(0, sum([0 if y == '' else 4 if x == y \
9+
else - 1 for x, y in zip(arr1, arr2)]))
10+
11+
print(check_exam(['a', 'a', 'c', 'b'], ['a', 'a', 'b', ''])) # Outputs - 7
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
print('If you can\'t sleep, just count sheep!!')
2+
print()
3+
4+
def count_sheep(n):
5+
6+
# This function will count the amount of sheep based in any given number.
7+
return ''.join([str(number) + ' sheep...' for number in range(1, n+1)])
8+
9+
print(count_sheep(3)) # Outputs - 1 sheep...2 sheep...3 sheep...
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
print('From Code Wars')
2+
print()
3+
4+
class Fighter(object):
5+
def __init__(self, name, health, damage_per_attack):
6+
self.name = name
7+
self.health = health
8+
self.damage_per_attack = damage_per_attack
9+
10+
def __str__(self): return "Fighter({}, {}, {})"\
11+
.format(self.name, self.health, self.damage_per_attack)
12+
__repr__ = __str__
13+
14+
def declare_winner(fighter1, fighter2, first_attacker):
15+
cur, opp = (fighter1, fighter2) if first_attacker == fighter1.name \
16+
else (fighter2, fighter1)
17+
while cur.health > 0:
18+
opp.health -= cur.damage_per_attack
19+
cur, opp = opp, cur
20+
return opp.name
21+
22+
print(declare_winner(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Lew"))

0 commit comments

Comments
 (0)