Some simple CodeWars problems
Spoiler Alert!
This post contains solutions to problems on CodeWars
On to CodeWars!
https://www.codewars.com/kata/550554fd08b86f84fe000a58/train/python
Given list of strings a1
and a2
, write a function in_array(a1,a2)
that returns a sorted list r
of which strings in a1
are substrings of strings in a2
.
It did not take me long.
def in_array(a1, a2):
r = set()
for s1 in a1:
for s2 in a2:
if s2.find(s1)!=-1:
r.add(s1)
return sorted(list(r))
Now we're gonna time this one.
https://www.codewars.com/kata/simple-pig-latin/train/python
pig_it(str)
takes a string str
and returns a new string with each word piglatinized.
I'm gonna blaze up first then record it.
View me solving and explaining this problem on Youtube
import string
def pig_it(s):
# take each word from 's'
# move the first letter in 's' to the end of the word
# then add 'ay' to the end of that word
# then add that word and a whitespace to the end of the return string
retval = ''
for w in s.split():
if w not in string.punctuation:
piglatin_word = w[1:] + w[0] + 'ay'
retval+= (piglatin_word + ' ')
else:
retval+=(w)
retval = retval.rstrip().lstrip()
return retval
Not my best work but I did it in under 10 minutes and on video.
This one is a bit tougher...
https://www.codewars.com/kata/calculating-with-functions/train/python
I knew this stuff would involve lamdba...dammit, lambda, it is a lamb, da!
lambda
lambda
lambda
I know what the problem is there for me and just tweeted about it here
This is a good feedback loop for communicating with the world. Anyways, onto the code.
If you'd like an explanation of how this was solved, I made another video located here on Youtube.
def zero(f=None): #your code here
#if f is None:
# return 0
#return f(0)
return 0 if f is None else f(0)
def one(f=None): #your code here
#if f is None:
# return 1
#return f(1)
return 1 if f is None else f(1)
def two(f=None): #your code here
#if f is None:
# return 2
#return f(2)
return 2 if f is None else f(2)
def three(f=None): #your code here
#if f is None:
# return 3
#return f(3)
return 3 if f is None else f(3)
def four(f=None): #your code here
#if f is None:
# return 4
#return f(4)
return 4 if f is None else f(4)
def five(f=None): #your code here
#if f is None:
# return 5
#return f(5)
return 5 if f is None else f(5)
def six(f=None): #your code here
#if f is None:
# return 6
#return f(6)
return 6 if f is None else f(6)
def seven(f=None): #your code here
#if f is None:
# return 7
#return f(7)
return 7 if f is None else f(7)
def eight(f=None): #your code here
#if f is None:
# return 8
#return f(8)
return 8 if f is None else f(8)
def nine(f=None): #your code here
#if f is None:
# return 9
#return f(9)
return 9 if f is None else f(9)
def plus(x): #your code here
return lambda y: y + x
def minus(x):
return lambda y: y - x
def times(x):
return lambda y: y * x
def divided_by(x):
return lambda y: int(y / x)