Skip to content

Commit f4fa907

Browse files
committed
Codewars kata
1 parent 97a5536 commit f4fa907

File tree

5 files changed

+230
-1
lines changed

5 files changed

+230
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
------------------------------ | ----------
1010
[Square into Squares protect trees!](https://www.codewars.com/kata/54eb33e5bc1a25440d000891/) | [Soluction python](python/4_kyu/Square_into_Squares_Protect_trees.py)
1111
[Nesting Structure Comparison](https://www.codewars.com/kata/520446778469526ec0000001/) | [Soluction python](python/4_kyu/Nesting_Structure_Comparison.py)
12+
[Sum by Factors](https://www.codewars.com/kata/54d496788776e49e6b00052f/) | [Soluction python](python/4_kyu/Sum_by_Factors.py)
1213

1314
**5-kyu**
1415
Problems/Kata | Solution
@@ -17,15 +18,18 @@
1718
[Write out numbers](https://www.codewars.com/kata/52724507b149fa120600031d/) | [Soluction python](python/5_kyu/Write_out_numbers.py)
1819
[Regex Password Validation](https://www.codewars.com/kata/52e1476c8147a7547a000811) | [Soluction python](python/5_kyu/Regex_Password_Validation.py)
1920
[Extract the domain name from a URL](https://www.codewars.com/kata/514a024011ea4fb54200004b/) | [Soluction python](python/5_kyu/Extract_the_domain_name_from_a_URL.py)
21+
[Help your granny!](https://www.codewars.com/kata/5536a85b6ed4ee5a78000035/) | [Soluction python](python/5_kyu/Help_your_granny!.py)
2022

2123
**6-kyu**
2224
Problems/Kata | Solution
2325
----------------------------- | --------
2426
[Longest Palindrome](https://www.codewars.com/kata/54bb6f887e5a80180900046b/train/python) | [Soluction python](python/6_kyu/longest_palindrome.py)
2527
[Help the bookseller!](https://www.codewars.com/kata/54dc6f5a224c26032800005c/train/python) | [Soluction python](python/6_kyu/Help_the_bookseller.py)
2628
[String array duplicates](https://www.codewars.com/kata/59f08f89a5e129c543000069/) | [Soluction python](python/6_kyu/String_array_duplicates.py)
29+
[Array diff](https://www.codewars.com/kata/523f5d21c841566fde000009/train/javascript) | [javascript](javascript/6_kyu/Array_diff.js)
2730

2831
**7-kyu**
2932
Problems/Kata | Solution
3033
------------------------------ | ----------
31-
[Numbers in strings](https://www.codewars.com/kata/59dd2c38f703c4ae5e000014/train/javascript) | [Soluction javascript](javascript/7_kyu/Numbers_in_strings.js)
34+
[Numbers in strings](https://www.codewars.com/kata/59dd2c38f703c4ae5e000014/train/javascript) | [Soluction javascript](javascript/7_kyu/Numbers_in_strings.js)
35+
[Isograms](https://www.codewars.com/kata/54ba84be607a92aa900000f1/) | [Soluction javascript](javascript/7_kyu/Isograms.js)

javascript/6_kyu/Array_diff.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Kata link:
2+
// https://www.codewars.com/kata/523f5d21c841566fde000009/
3+
4+
// -------------------------------------
5+
// Instructions:
6+
/*
7+
Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
8+
9+
It should remove all values from list a, which are present in list b.
10+
11+
arrayDiff([1,2],[1]) == [2]
12+
If a value is present in b, all of its occurrences must be removed from the other:
13+
14+
arrayDiff([1,2,2,2,3],[2]) == [1,3]
15+
*/
16+
// -------------------------------------
17+
// Solution:
18+
19+
function arrayDiff(a, b) {
20+
if (a.length > 0 && b.length > 0) {
21+
return a.filter(x => !b.includes(x));
22+
}
23+
return a;
24+
}
25+
// -------------------------------------
26+
// Basic Tests
27+
console.log(arrayDiff([], [4,5]), [], "a was [], b was [4,5]");
28+
console.log(arrayDiff([3,4], [3]), [4], "a was [3,4], b was [3]");
29+
console.log(arrayDiff([1,8,2], []), [1,8,2], "a was [1,8,2], b was []");
30+
console.log(arrayDiff([1,2,2,2,3],[2]));

javascript/7_kyu/Isograms.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Kata link:
2+
// https://www.codewars.com/kata/59dd2c38f703c4ae5e000014/train/javascript
3+
4+
// -------------------------------------
5+
// Instructions:
6+
/*
7+
An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.
8+
9+
isIsogram("Dermatoglyphics") == true
10+
isIsogram("aba") == false
11+
isIsogram("moOse") == false // -- ignore letter case
12+
*/
13+
// -------------------------------------
14+
// Solution:
15+
16+
function isIsogram(str){
17+
if (str.length == 0) { return true; }
18+
else {
19+
let ar = str.toLowerCase().split("");
20+
let arr = Array.from(ar);
21+
const repeat = (item) => arr.slice(ar.indexOf(item)+1).includes(item);
22+
return ar.every(repeat);
23+
}
24+
}
25+
26+
27+
// -------------------------------------
28+
// Basic Tests
29+
console.log( isIsogram("Dermatoglyphics"), true );
30+
console.log( isIsogram("isogram"), true );
31+
console.log( isIsogram("aba"), false, "same chars may not be adjacent" );
32+
console.log( isIsogram("moOse"), false, "same chars may not be same case" );
33+
console.log( isIsogram("isIsogram"), false );
34+
console.log( isIsogram(""), true, "an empty string is a valid isogram" );

python/4_kyu/Sum_by_Factors.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Kata link:
2+
# https://www.codewars.com/kata/54d496788776e49e6b00052f/
3+
4+
# -------------------------------------
5+
# Instructions:
6+
'''
7+
Given an array of positive or negative integers
8+
9+
I= [i1,..,in]
10+
11+
you have to produce a sorted array P of the form
12+
13+
[ [p, sum of all ij of I for which p is a prime factor (p positive) of ij] ...]
14+
15+
P will be sorted by increasing order of the prime numbers. The final result has to be given as a string in Java, C#, C, C++ and as an array of arrays in other languages.
16+
17+
Example:
18+
19+
I = [12, 15] # result = [[2, 12], [3, 27], [5, 15]]
20+
[2, 3, 5] is the list of all prime factors of the elements of I, hence the result.
21+
22+
Notes:
23+
24+
It can happen that a sum is 0 if some numbers are negative!
25+
Example: I = [15, 30, -45] 5 divides 15, 30 and (-45) so 5 appears in the result, the sum of the numbers for which 5 is a factor is 0 so we have [5, 0] in the result amongst others.
26+
27+
In Fortran - as in any other language - the returned string is not permitted to contain any redundant trailing whitespace: you can use dynamically allocated character strings.
28+
'''
29+
30+
# -------------------------------------
31+
# Solution:
32+
# Note: Don't work in all test
33+
34+
def sum_for_list(lst):
35+
def simple_abs(num):
36+
return -num if num < 0 else num
37+
abs_value_lst = []
38+
for i in lst:
39+
value = simple_abs(i)
40+
abs_value_lst.append(value)
41+
largest = max(abs_value_lst)
42+
43+
p = []
44+
count = 2
45+
46+
while count < largest:
47+
isprime = True
48+
for x in range(2, int((count/2) + 1)):
49+
if count % x == 0:
50+
isprime = False
51+
break
52+
if isprime:
53+
p.append(count)
54+
count += 1
55+
56+
sum_list = []
57+
sum = 0
58+
counter = 0
59+
60+
for j in p:
61+
sum = 0
62+
counter = 0
63+
for i in lst:
64+
if not i % j:
65+
sum += i
66+
counter += 1
67+
if counter:
68+
sum_list.append([j, sum])
69+
return sum_list
70+
71+
# -------------------------------------
72+
# Basic Tests:
73+
74+
a = [12, 15]
75+
print(sum_for_list(a), '=', [[2, 12], [3, 27], [5, 15]])

python/5_kyu/Help_your_granny!.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Kata link:
2+
# https://www.codewars.com/kata/5536a85b6ed4ee5a78000035/
3+
4+
# -------------------------------------
5+
# Instructions:
6+
'''
7+
Your granny, who lives in town X0, has friends. These friends are given in an array, for example: array of friends is
8+
9+
[ "A1", "A2", "A3", "A4", "A5" ].
10+
The order of friends is this array must not be changed since this order gives the order in which they will be visited.
11+
12+
These friends inhabit towns and you have an array with friends and towns, for example:
13+
14+
[ ["A1", "X1"], ["A2", "X2"], ["A3", "X3"], ["A4", "X4"] ]
15+
or
16+
[ ("A1", "X1"), ("A2", "X2"), ("A3", "X3"), ("A4", "X4") ]
17+
or
18+
(C)
19+
{"A1", "X1", "A2", "X2", "A3", "X3", "A4", "X4"}
20+
which means A1 is in town X1, A2 in town X2... It can happen that we don't know the town of one of the friends.
21+
22+
Your granny wants to visit her friends and to know how many miles she will have to travel.
23+
24+
You will make the circuit that permits her to visit her friends. For example here the circuit will contain:
25+
26+
X0, X1, X2, X3, X4, X0
27+
and you must compute the total distance
28+
29+
X0X1 + X1X2 + .. + X4X0.
30+
For the distance, fortunately, you have a map (and a hashmap) that gives each distance X0X1, X0X2 and so on. For example:
31+
32+
[ ["X1", 100.0], ["X2", 200.0], ["X3", 250.0], ["X4", 300.0] ]
33+
or
34+
Map("X1" -> 100.0, "X2" -> 200.0, "X3" -> 250.0, "X4" -> 300.0)
35+
or (Coffeescript, Javascript)
36+
['X1',100.0, 'X2',200.0, 'X3',250.0, 'X4',300.0 ]
37+
or
38+
(C)
39+
{"X1", "100.0", "X2", "200.0", "X3", "250.0", "X4", "300.0"}
40+
which means that X1 is at 100.0 miles from X0, X2 at 200.0 miles from X0, etc...
41+
42+
More fortunately (it's not real life, it's a story...), the towns X0, X1, ..Xn are placed in the following manner:
43+
44+
X0X1X2 is a right triangle with the right angle in X1, X0X2X3 is a right triangle with the right angle in X2, etc...
45+
46+
If a town Xi is not visited you will suppose that the triangle
47+
48+
X0Xi-1Xi+1 is still a right triangle.
49+
50+
(Ref: https://en.wikipedia.org/wiki/Pythagoras#Pythagorean_theorem)
51+
52+
Task
53+
Can you help your granny and give her the distance to travel?
54+
55+
Notes
56+
If you have some difficulty to see the tour I made a non terrific but maybe useful drawing:
57+
(https://i.imgur.com/dG7iWXhm.jpg)
58+
All languages
59+
See the data type of the parameters in the examples test cases.
60+
61+
Towns can have other names that X0, X1, X2, ... Xn
62+
63+
"tour" returns an int which is the floor of the total distance.
64+
'''
65+
# -------------------------------------
66+
# Solution:
67+
from math import sqrt, floor
68+
69+
def tour(friends, friend_towns, home_to_town_distances):
70+
distance = 0
71+
n = [home_to_town_distances[t[1]] for f in friends for t in friend_towns if f == t[0]]
72+
index = 0
73+
for i in n:
74+
if index < len(n) - 1:
75+
distance += sqrt(round(abs(pow(i,2) - pow(n[index + 1],2))))
76+
index += 1
77+
return floor(distance + n[0] + n[-1])
78+
79+
80+
81+
# -------------------------------------
82+
# Basic Tests
83+
friends1 = ["A1", "A2", "A3", "A4", "A5"]
84+
fTowns1 = [["A1", "X1"], ["A2", "X2"], ["A3", "X3"], ["A4", "X4"]]
85+
distTable1 = {"X1": 100.0, "X2": 200.0, "X3": 250.0, "X4": 300.0}
86+
print(tour(friends1, fTowns1, distTable1), 889)

0 commit comments

Comments
 (0)