Skip to content

Instantly share code, notes, and snippets.

@SlyCodePanda
Last active February 18, 2020 00:52
Show Gist options
  • Save SlyCodePanda/7b75b369225c6249200fa322a424544c to your computer and use it in GitHub Desktop.
Save SlyCodePanda/7b75b369225c6249200fa322a424544c to your computer and use it in GitHub Desktop.
A bunch of Code Wars challenges I have done. Frist function is mine, second is best voted answer.
'''
create a method that can determine how many letters and digits are in a given string.
Example:
"hel2!lo" --> 6
"wicked .. !" --> 6
"!?..A" --> 1
'''
def count_letters_and_digits(s):
word = [text for text in s if text.isalpha() or text.isdigit()]
return len(word)
#################
# Best Solution #
#################
def count_letters_and_digits(s):
return isinstance(s, str) and sum(map(str.isalnum, s))
'''
Delete occurrences of an element if it occurs more than n times.
Example:
delete_nth ([1,1,1,1],2) # return [1,1]
delete_nth ([20,37,20,21],1) # return [20,37,21]
'''
def delete_nth(order,max_e):
for index in range(len(order)-1, 1, -1):
if (order.count(order[index])) > max_e:
del order[index]
return order
#################
# Best Solution #
#################
def delete_nth(order,max_e):
ans = []
for o in order:
if ans.count(o) < max_e: ans.append(o)
return ans
import re
def extract_domain(url):
print url.split("//")[-1].split("www.")[-1].split(".")[0]
print re.search('(https?://)?(www\d?\.)?(?P<name>[\w-]+)\.', url).group('name')
extract_domain('www.xakep.ru')
extract_domain('http://thecodeinn.blogspot.com/2013/07/tutorial-pyqt-digital-clock.html')
extract_domain('https://www.quora.com/How-do-I-extract-only-the-domain-name-from-an-URL')
def highest_rank(arr):
highestValue = {}
for num in arr:
highestValue.update({arr.count(num): num})
return highestValue.get(max(highestValue.keys()))
'''
Example:
wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
'''
def wave(str):
# output = []
#
# for num in range(len(str)):
# if str[num] != ' ':
# output.append((str[:num].lower() + str[num:].capitalize()))
#
# return output
return [(str[:num].lower() + str[num:].capitalize()) for num in range(len(str)) if str[num] != ' ']
#################
# Best Solution #
#################
def wave(str):
return [str[:i] + str[i].upper() + str[i+1:] for i in range(len(str)) if str[i].isalpha()]
import re
def increment_string(strng):
# If strng is empty return 1.
try:
checkString = strng[-1].isdigit()
except IndexError:
return '1'
# If the string already ends with a number, the number should be incremented by 1.
if checkString is True:
# Get num from end of string.
fullNum = re.search(r'\d+$', strng).group(0)
length = len(fullNum)
num = int(fullNum)+1
finalNum = ("%0"+"%dd" % length) % num
# Remove the found ending number from the string.
stringBit = strng.replace(fullNum, "", 1)
# Return string with new number on the end.
return stringBit + str(finalNum)
# If the string does not end with a number. the number 1 should be appended to the new string.
else:
return strng + '1'
#################
# Best solution #
#################
def increment_string(strng):
head = strng.rstrip('0123456789')
tail = strng[len(head):]
if tail == "": return strng+"1"
return head + str(int(tail) + 1).zfill(len(tail))
'''
Write a function which takes a list of strings and returns each line prepended by the correct number.
The numbering starts at 1. The format is n: string. Notice the colon and space in between.
Examples:
number([]) # => []
number(["a", "b", "c"]) # => ["1: a", "2: b", "3: c"]
'''
def number(lines):
return ["%s: %s" % (str(index+1), lines[index]) for index in range(len(lines))]
#################
# Best Solution #
#################
def number(lines):
return ['%d: %s' % v for v in enumerate(lines, 1)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment