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

Diff for: Challenges/BasicChallenges_7kyu/README.md

+2
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.

Diff for: Challenges/BasicChallenges_7kyu/add_binary.py

+13
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

Diff for: Challenges/BasicChallenges_7kyu/arithmetic.py

+17
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

Diff for: Challenges/BasicChallenges_7kyu/calculate_years.py

+16
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.

Diff for: Challenges/BasicChallenges_7kyu/capitalize.py

+11
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']

Diff for: Challenges/BasicChallenges_7kyu/capitals.py

+10
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]

Diff for: Challenges/BasicChallenges_7kyu/check_coupon.py

+15
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

Diff for: Challenges/BasicChallenges_7kyu/check_exam.py

+11
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

Diff for: Challenges/BasicChallenges_7kyu/count_sheep.py

+9
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...

Diff for: Challenges/BasicChallenges_7kyu/declare_winner.py

+22
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"))

Diff for: Challenges/BasicChallenges_7kyu/diff_array.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def number(bus_stops):
5+
6+
# This function calculates the difference between two numbers inside a list,
7+
# nested in another list and return the total.
8+
return sum([num[0] - num[1] for num in bus_stops])
9+
10+
print(number([[10, 0], [3, 5], [5, 8]])) # Outputs - 5

Diff for: Challenges/BasicChallenges_7kyu/divisors.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def divisors(integer):
5+
6+
# This function returns the divisors of any given number,
7+
# or the number itself if it's prime number.
8+
a = [num for num in range(2, integer) if integer % num == 0]
9+
return a if len(a) != 0 else f'{integer} is prime.'
10+
# An empty list equals to false.
11+
12+
print(divisors(12)) # Outputs - [2, 3, 4, 6]
13+
print(divisors(13)) # Outputs - 13 is prime.

Diff for: Challenges/BasicChallenges_7kyu/divisorsa.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def divisors(n):
5+
6+
# This function counts the number of divisors of an integer.
7+
return len([num for num in range(1, n + 1) if n % num == 0])
8+
# return sum(1 for i in xrange(1, n + 1) if n % i == 0) - Other way.
9+
10+
print(divisors(4)) # Outputs - [1, 2, 4] - 3
11+
print(divisors(30)) # Outputs - [1, 2, 3, 5, 6, 10, 15, 30] - 8

Diff for: Challenges/BasicChallenges_7kyu/dna_dict.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def dna_strand(dna):
5+
6+
# This function replaces letters of dna.
7+
dna_pair = \
8+
{
9+
'A': 'T',
10+
'T': 'A',
11+
'C': 'G',
12+
'G': 'C'
13+
}
14+
return ''.join([dna_pair[let] for let in dna])
15+
16+
print(dna_strand('AGAT')) # Outputs - TCTA

Diff for: Challenges/BasicChallenges_7kyu/dont_five.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def dont_give_me_five(start, end):
5+
6+
# This function counts how many numbers are in a range without '5' in it.
7+
return len([num for num in range(start, end + 1) if '5' not in str(num)])
8+
9+
print(dont_give_me_five(4, 17)) # Output - 12

Diff for: Challenges/BasicChallenges_7kyu/endswith.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def solution(string, ending):
5+
6+
# This function checks if the first argument ends with the second.
7+
return string.endswith(ending)
8+
# The endswith() method returns True if the string ends\
9+
# with the specified value, otherwise False.
10+
11+
print(solution('abcde', 'cde')) # Outputs - True
12+
print(solution('abcde', 'abc')) # Outputs - False

Diff for: Challenges/BasicChallenges_7kyu/evaporator.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def evaporator(content, evap_per_day, threshold):
5+
6+
# This program tests the life of an evaporator containing a gas.
7+
days = 0
8+
limit = content * threshold/ 100
9+
while content > limit:
10+
content = content - (content * evap_per_day/ 100)
11+
days += 1
12+
return days
13+
14+
print(evaporator(10, 10, 5)) # Outputs - 29 Days.

Diff for: Challenges/BasicChallenges_7kyu/factorial.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def factorial(n):
5+
6+
# This function will calculate the factorial of any given
7+
# number between 1 and 12, and trow an error if the number
8+
# is not between this interval.
9+
fac = 1
10+
if n < 0 or n > 12:
11+
raise ValueError
12+
else:
13+
for num in range(1, n + 1):
14+
fac *= num
15+
return fac
16+
17+
print(factorial(12)) # Outputs - 479001600
18+
print(factorial(13)) # Outputs - ValueError

Diff for: Challenges/BasicChallenges_7kyu/filter_letter.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def printer_error(s):
5+
6+
# This function counts letters above letter 'n' in a string,\
7+
# and show the result beside the total amount of letters.
8+
return str(len([let for let in s if let > 'm'])) + '/' + str(len(s))
9+
# return '{}/{}'.format(len([let for let in s if let > 'm']), len(s))
10+
11+
print(printer_error('aaaxbbbbyyhwawiwjjjwwm')) # Outputs - 8/22

Diff for: Challenges/BasicChallenges_7kyu/filter_list.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def filter_list(l):
5+
6+
# This function removes strings from a list with strings and integers.
7+
return [num for num in l if type(num) != str]
8+
9+
print(filter_list([1, 2, 3, 'a', 'kajdçlkjfa'])) # Outputs - [1, 2, 3]

Diff for: Challenges/BasicChallenges_7kyu/filter_lista.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def friend(x):
5+
6+
# This function maintains only names with 4 letters.
7+
return [name for name in x if name.isalpha() and len(name) == 4]
8+
9+
names = ["Ryan", "Kieran", "Jason", "Yous", '123']
10+
print(friend(names)) # Outputs - ['Ryan', 'Yous']

Diff for: Challenges/BasicChallenges_7kyu/find.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def find(n):
5+
6+
# This function will return the sum of all multiples of 3 and 5,\
7+
# of any given number.
8+
return [num for num in range(1, n + 1) if num % 3 == 0 or num % 5 == 0]
9+
10+
print(find(5)) # Outputs - 8 [3, 5]
11+
print(find(10)) # Outputs - 33 [3, 5, 6, 9, 10]

Diff for: Challenges/BasicChallenges_7kyu/find_short.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def find_short(s):
5+
6+
# This function returns the shortest word's length from a group of words.
7+
return len(min(s.split(), key=len))
8+
9+
print(find_short('Lets all go on holiday somewhere very cold')) # Outputs - 2

Diff for: Challenges/BasicChallenges_7kyu/find_square.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from math import sqrt, isqrt
2+
print('From Code Wars')
3+
4+
def find_next_square(sq):
5+
6+
# Given an integer 'sq', it returns -1 for no square numbers,\
7+
# and the next square number otherwise.
8+
return -1 if sqrt(sq) != isqrt(sq) else (sqrt(sq) + 1) ** 2
9+
10+
print(find_next_square(25)) # Outputs - 36
11+
print(find_next_square(26)) # Outputs - -1

Diff for: Challenges/BasicChallenges_7kyu/flatten_and_sort.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def flatten_and_sort(array):
5+
6+
# This function takes a 2D array, and return a flatten
7+
# version with all elements in ascending order.
8+
return sorted([j for sub in array for j in sub])
9+
# return sorted(sum(array, [])) - Clever.
10+
11+
print(flatten_and_sort([[1, 3, 5], [100], [2, 4, 6]]))
12+
# Outputs - [1, 2, 3, 4, 5, 6, 100]

Diff for: Challenges/BasicChallenges_7kyu/fut_pop.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def nb_year(p0, percent, aug, p):
5+
6+
# This function calculates how many years\
7+
# it necessary to reach a future population.
8+
year = 0
9+
while p0 < p:
10+
p0 += int(p0 * (percent/100) + aug)
11+
year += 1
12+
return year
13+
14+
print(nb_year(1000, 2, 50, 1200))
15+
# Outputs - 3 years. It means,\ starting with a population\
16+
# of 1000 it will take 3 years to reach a population of 1200.

Diff for: Challenges/BasicChallenges_7kyu/get_middle.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def get_middle(s):
5+
6+
# This function returns two middle characters of a string
7+
# if the length is even, and only one middle character if
8+
# the string length is odd.
9+
a = int(len(s) / 2)
10+
return s[a - 1] + s[a] if len(s) % 2 == 0 else s[a]
11+
12+
print(get_middle('Toll')) # Outputs - ol

Diff for: Challenges/BasicChallenges_7kyu/in_asc.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def in_asc_order(arr):
5+
6+
# This function determines whether a list is or not in ascending order.
7+
return arr == sorted(arr)
8+
9+
print(in_asc_order([1, 2, 4, 7, 19])) # Outputs - True
10+
print(in_asc_order([1, 6, 10, 18, 2, 4, 20])) # Outputs - False

Diff for: Challenges/BasicChallenges_7kyu/is_anagram.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def is_anagram(test, original):
5+
6+
# This function determines whether two words are anagram.
7+
# Anagram definition : a word, phrase, or name formed by rearranging\
8+
# the letters of another,\ such as cinema, formed from iceman.
9+
return sorted(test.lower()) == sorted(original.lower())
10+
11+
print(is_anagram('Buckethead', 'DeathCubeK')) # Outputs - True.
12+
print(is_anagram('dumble', 'bumble')) # Outputs - False.

Diff for: Challenges/BasicChallenges_7kyu/is_isogram.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
print('From Code Wars')
2+
print()
3+
4+
def is_isogram(string):
5+
6+
# This function determines if a string is or not a isogram.
7+
return True if len(set(string.lower())) == len(list(string.lower()))\
8+
else False
9+
10+
print(is_isogram('moOse')) # Outputs - False
11+
print(is_isogram('isogram')) # Outputs - True
12+
'''
13+
An isogram is a word that has no repeating letters, consecutive
14+
or non-consecutive. Implement a function that determines whether
15+
a string that contains only letters is an isogram. Assume the empty
16+
string is an isogram. Ignore letter case.
17+
'''

0 commit comments

Comments
 (0)