Skip to content

Instantly share code, notes, and snippets.

@kurzweil777
kurzweil777 / Vasya_Clerk
Created July 8, 2020 04:07
exercise_from_codewars
def tickets(numbers):
"""The new "Avengers" movie has just been released! There are a lot of people at the cinema box office standing
in a huge line. Each of them has a single 100, 50 or 25 dollar bill. An "Avengers" ticket costs 25 dollars.
Return YES, if Vasya can sell a ticket to every person and give change with the bills he has at hand at that
moment. Otherwise return NO """
money = []
for number in numbers:
if number == 25:
money.append(number)
try:
@kurzweil777
kurzweil777 / BreakCamelCase
Created July 8, 2020 05:25
Exercise from code_wars
import re
def camel(phrase):
"""Complete the solution so that the function will break up camel casing, using a space between words."""
regex = re.compile(r'''[A-Z]''')
words = [d for d in phrase]
for word in regex.finditer(phrase):
if " " in words:
# Because of the first insert of " " the indexes have been moved.
@kurzweil777
kurzweil777 / Format Names
Created July 8, 2020 07:20
Exercise from code_wars
def namelist(names):
"""
Given: an array containing hashes of names
Return: a string formatted as a list of names separated by commas except for the last two names, which should be
separated by an ampersand. More info: https://bit.ly/2O1dmvs"""
list_of_names = [value for name in names for key, value in name.items()]
if len(list_of_names) >= 2:
# Replacing comma in the end of string with ampersand.
return (', '.join(list_of_names)[::-1].replace(",", "& ", 1))[::-1]
else:
@kurzweil777
kurzweil777 / Working with shelve exercise
Created July 19, 2020 16:02
Exercise from automate boring stuff
# ! python3
# mcb.pyw - saves and loads a pieces of text
# to the clipboard.
# Usage: py.exe mcb.pyw save <keyword> -
# Saves the clipboard to a keyword
# py.exe mcb pyw <keyword> - \
# Loads a keyword to the clipboard
# py.exe mcb.pyw list - \
# Loads all keywords
# mcb.pyw delete <keyword> - \
@kurzweil777
kurzweil777 / Mad Libs
Last active July 21, 2020 13:06
Exercise from automate boring stuff
# Create a Mad Libs program that reads in text files and lets the user add
# their own text anywhere the word ADJECTIVE, NOUN, ADVERB, or VERB
# appears in # the text file. For example, a text file may look like this:
# The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was
# unaffected by these events.
# The program would find these occurrences and prompt the user to replace them.
with open("ad.txt", "r") as file:
@kurzweil777
kurzweil777 / Regex Search
Created July 21, 2020 13:46
Exercise from automate_boring stuff
@kurzweil777
kurzweil777 / Selective Copy
Created July 23, 2020 04:47
Exercise from automate boring stuff
#!python3
"""Write a program that walks through a folder tree and searches for files with a certain fileextension (such as
.pdf
or
.jpg).
Copy these files from whatever location they are in to a new folder."""
import os
import shutil
@kurzweil777
kurzweil777 / Deleting unneeded files
Last active July 23, 2020 18:26
Exercise from automate boring stuff
import os
"""Write a program that walks through a folder tree and searches for exceptionally large files or folders — say,
ones that have a file size of more than 100MB.
Print these files with their absolute path to the screen. """
flag = False
for foldername, subfolder, filename in os.walk(r"C:\Users\python\Desktop"):
folders = [foldername + "\\" + folder for folder in filename]
user_answers_yes = ["y", "yes", "yep", "yeah"]
@kurzweil777
kurzweil777 / Filling in the Gaps
Last active July 25, 2020 13:11
Exercise from automate boring stuff
#!python3
import os
import re
"""Write a program that finds all files with a given prefix, such as spam001.txt , spam002.txt ,
and so on, in a single folder and locates any gaps in the numbering (such as if there is a
spam001.txt and spam003.txt but no spam002.txt ).
Have the program rename all the later files to close this gap."""
@kurzweil777
kurzweil777 / Creating the Gaps
Last active July 25, 2020 13:14
Exercise from automate boring stuff
#!python3
import os
import re
"""As an added challenge, write another program that can insert gaps into numbered files so that a new file can be
added. """
working_directory = input("Input your working directory with files: ")
numbers = [[correct_number + 1, number_of_file] for foldername, subfolder, filename in