Skip to content

Instantly share code, notes, and snippets.

@guestPK1986
guestPK1986 / Counter_defaultdict_OrderedDict.py
Last active September 15, 2020 21:06
Counter_defaultdict_OrderedDict.py
from collections import Counter, defaultdict, OrderedDict
li = [1,2,3,4,5,6,7,7,7]
sentance = "blah blah blah thinking about python"
print(Counter(li)) # will return Counter({7: 3, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1})
print(Counter(sentance)) # will return Counter({'h': 5, ' ': 5, 'b': 4, 'a': 4, 'l': 3, 't': 3, 'n': 3, 'i': 2, 'o': 2, 'k': 1, 'g': 1, 'u': 1, 'p': 1, 'y': 1})
dictionary = defaultdict(lambda: "does not exist", {"a":1, "b": 2})
print(dictionary['c']) # will return 'does not exist'
@guestPK1986
guestPK1986 / comprehensions.py
Created August 30, 2020 20:32
list, set, dictionary comprehension exercises
#create a LIST of characters in string 'hello'
my_list = [i for i in 'hello']
print("my_list "+ str(my_list))
#create a LIST of numbers in tuple (1,2,3,4)
new_tuple = [i for i in (1,2,3,4)]
print("new_tuple " + str(new_tuple))
#create a SET of unique items from string
a_set = {i for i in 'hello'}
@guestPK1986
guestPK1986 / map_filter_zip_reduce.py
Last active August 30, 2020 19:44
map(), filter(), zip(), reduce(), lambda
#1 Capitalize first letter of all of the pet names and print the list
my_pets = ['sisi', 'bibi', 'titi', 'carla']
def first_upper(item):
for x in item:
up_char = item[0].upper()
item = up_char + item[1:]
return item
print(list(map(first_upper, my_pets)))
@guestPK1986
guestPK1986 / functions_classes.py
Last active August 30, 2020 15:46
python functions, classes
# Create a class Cat which will define name and age of cat
# Instantiate the Cat object with 3 cats
# Create a function that finds the oldest cat
# Print out: "The oldest cat is x years old.". x will be the oldest cat age by using the function
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
@guestPK1986
guestPK1986 / bash-cheatsheet.sh
Created August 26, 2020 14:23 — forked from LeCoupa/bash-cheatsheet.sh
Bash CheatSheet for UNIX Systems --> UPDATED VERSION --> https://github.com/LeCoupa/awesome-cheatsheets
#!/bin/bash
#####################################################
# Name: Bash CheatSheet for Mac OSX
#
# A little overlook of the Bash basics
#
# Usage:
#
# Author: J. Le Coupanec
# Date: 2014/11/04
#Provided is a list of tuples. Create another list called t_check that contains the third element of every tuple.
lst_tups = [('Articuno', 'Moltres', 'Zaptos'), ('Beedrill', 'Metapod', 'Charizard', 'Venasaur', 'Squirtle'), ('Oddish', 'Poliwag', 'Diglett', 'Bellsprout'), ('Ponyta', "Farfetch'd", "Tauros", 'Dragonite'), ('Hoothoot', 'Chikorita', 'Lanturn', 'Flaaffy', 'Unown', 'Teddiursa', 'Phanpy'), ('Loudred', 'Volbeat', 'Wailord', 'Seviper', 'Sealeo')]
t_check = []
for x in lst_tups:
t_check.append(x[2])
print (t_check)
#Below, we have provided a list of tuples.
@guestPK1986
guestPK1986 / functions.py
Created August 23, 2020 01:25
python functions
#Write a function named total that takes a list of integers as input,
#and returns the total value of all those integers added together.
def total(lst):
accum=0
for x in lst:
accum=accum+x
return accum
lst=[1,2,3,4]
total(lst)
@guestPK1986
guestPK1986 / course_1_assessment_9_py
Created August 21, 2020 23:15
course_1_assessment_9
# 1. Write code to add ‘horseback riding’ to the third position (i.e., right before volleyball) in the list sports.
sports = ['cricket', 'football', 'volleyball', 'baseball', 'softball', 'track and field', 'curling', 'ping pong', 'hockey']
sports.insert(2, 'horseback riding')
# 2. Write code to take ‘London’ out of the list trav_dest.
trav_dest = ['Beirut', 'Milan', 'Pittsburgh', 'Buenos Aires', 'Nairobi', 'Kathmandu', 'Osaka', 'London', 'Melbourne']
trav_dest.pop(7)
@guestPK1986
guestPK1986 / course_1_assessment_8.py
Created August 19, 2020 04:23
course_1_assessment_8
#Create one conditional so that if “Friendly” is in w, then “Friendly is here!” should be assigned to the variable wrd.
#If it’s not, check if “Friend” is in w. If so, the string “Friend is here!” should be assigned to the variable wrd,
#otherwise “No variation of friend is in here.” should be assigned to the variable wrd
w = "Friendship is a wonderful human experience!"
for x in w:
if "Friendly" in w:
wrd = str("Friendly is here!")
elif "Friend" in w:
wrd = str("Friend is here!")
@guestPK1986
guestPK1986 / course_1_assessment_7.py
Created August 19, 2020 02:09
course_1_assessment_7
#rainfall_mi is a string that contains the average number of inches of rainfall in Michigan for every month (in inches)
#with every month separated by a comma.
#Write code to compute the number of months that have more than 3 inches of rainfall.
#Store the result in the variable num_rainy_months. In other words, count the number of items with values > 3.0
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
file = rainfall_mi.split(",")
print(file)
num_rainy_months = 0
for x in file: