Skip to content

Instantly share code, notes, and snippets.

def basic_op(operator, value1, value2):
""" Your task is to create a function that does four basic mathematical operations.
The function should take three arguments - operation(string/char), value1(number), value2(number).
The function should return result of numbers after applying the chosen operation."""
return eval("{}{}{}".format(value1, operator, value2))
basic_op('+', 2, 3) # 5
import difflib
from difflib import Differ
string1 = 'gvgbf'
string2 = 'gvgbf'
def difference(first_string, second_string):
"""This function compares two strings between each other and returns an answer whether there are some differences
or not"""
@kurzweil777
kurzweil777 / Create a phone number
Last active June 5, 2020 03:17
Exercise_from_CodeWars
def create_phone_number(n):
"""Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those
numbers in the form of a phone number. """
phone_numbers = ''.join(map(str, n))
return f'({phone_numbers[0:3]}) {phone_numbers[3:6]}-{phone_numbers[6:10]}'
print(create_phone_number([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])) # (123) 456-7890
from collections import Counter
def delete_nth(order, max_e):
"""Given a list lst and a number N, create a new list that contains each number of lst at most N times without
reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,
2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3]. """
numbers = []
c = Counter()
@kurzweil777
kurzweil777 / Square Digits
Created June 5, 2020 06:15
Exercise from Code_wars
def square_digits(num):
"""Welcome. In this kata, you are asked to square every digit of a number."""
new_numbers = []
for digit in str(num): # Iterating through the given string in num
digit = int(digit) # Converting strings into ints
new_numbers.append(digit * digit) # Adding calculated numbers into a new list
str_numbers = ''.join(map(str, new_numbers)) # Creating a string, made of new list
return int(str_numbers)
@kurzweil777
kurzweil777 / Jaden Case
Created June 5, 2020 10:55
Exercise from codeWars
def to_jaden_case(string):
"""Your task is to convert strings to how they would be written by Jaden Smith. The strings are actual quotes
from Jaden Smith, but they are not capitalized in the same way he originally typed them. """
capitalizing = [word.capitalize() for word in string.split()]
string = ' '.join(capitalizing)
return string
quote = "How can mirrors be real if our eyes aren't real"
@kurzweil777
kurzweil777 / Likes
Created June 5, 2020 11:57
Exercise from CodeWars
def likes(names):
"""Implement a function likes :: [String] -> String, which must take in input array, containing the names of
people who like an item. """
if len(names) == 0:
return 'no one likes this'
elif len(names) == 1:
return f'{names[0]} likes this'
elif len(names) == 2:
@kurzweil777
kurzweil777 / Bus Stops
Created June 7, 2020 18:01
Exercise from CodeWars
def number(bus_stops):
"""Your task is to return number of people who are still in the bus after the last bus station (after the last
array). Even though it is the last bus stop, the bus is not empty and some people are still in the bus,
and they are probably sleeping there :D """
bus_stops = dict(bus_stops)
inside_the_bus, outside_the_bus = [inside for inside in bus_stops.keys()], \
[outside for outside in bus_stops.values()]
total = sum(inside_the_bus) - sum(outside_the_bus)
return total
@kurzweil777
kurzweil777 / Remove Smallest
Created June 8, 2020 06:12
Exercise from CodeWars
def remove_smallest(numbers):
"""Given an array of integers, remove the smallest value. Do not mutate the original array/list. If there are
multiple elements with the same value, remove the one with a lower index. If you get an empty array/list,
return an empty array/list.
Don't change the order of the elements that are left."""
n_copy = numbers.copy() # Creating a copy of input string, to avoid mutation in the original one
try:
@kurzweil777
kurzweil777 / Tortoise racing
Created June 11, 2020 10:23
Exercise from CodeWars
def race(v1, v2, g):
"""Two tortoises named A and B must run a race. A starts with an average speed of 720 feet per hour. Young B
knows she runs faster than A, and furthermore has not finished her cabbage.
When she starts, at last, she can see that A has a 70 feet lead but B's speed is 850 feet per hour. How long will it
take B to catch A?
More info: https://bit.ly/2zobER9"""
if v1 > v2: