Skip to content

Instantly share code, notes, and snippets.

@DevShasa
DevShasa / reset.css
Created May 17, 2022 03:43
css reset
:root{
box-sizing: border-box;
}
*, ::before, ::after {
box-sizing: inherit;
}
* {margin: 0 }
body {
'''
Write a function that takes a list or tuple of numbers. Return the result of alter-
nately adding and subtracting numbers from each other. So calling the func-
tion as plus_minus([10, 20, 30, 40, 50, 60]) , you’ll get back the result of
10+20-30+40-50+60 , or 50
'''
# This is a somewhat ineffective solution
def plus_minus(my_list):
st = ''
@DevShasa
DevShasa / evem_odd.py
Last active March 15, 2021 11:57
Function that takes a list or tuple of numbers returns a two element list containing the sum of even indexed numbers and the sum of the odd indexed numbers
def even_odd_sums(my_list):
odd = 0
even = 0
for x in range(len(my_list)):
if x % 2 == 0:
even += my_list[x]
else:
odd += my_list[x]
return [even, odd]
@DevShasa
DevShasa / transpose.py
Created February 23, 2021 08:07
Write a function that transposes a list of strings, in which each string contains multiple words separated by whitespace. Specifically, it should perform in such a way that if you were to pass the list ['abc def ghi', 'jkl mno pqr', 'stu vwx yz'] to the function, it would return ['abc jkl stu', 'def mno vwx', 'ghi pqr yz'] .
def transposer(list_of_strings):
return [" ".join(y[z] for y in [x.split() for x in list_of_strings]) for z in range(len(list_of_strings))]
@DevShasa
DevShasa / pangram.py
Created February 15, 2021 20:08
Determine if a sentence is a pangram.
import string
def is_pangram(sentence):
return set(x for x in sentence.lower() if x in string.ascii_lowercase) == set(string.ascii_lowercase)
@DevShasa
DevShasa / matrix.py
Created February 15, 2021 12:39
Given a string with embedded newlines representing a matrix of numbers, return the rows and columns of that matrix.
class Matrix:
def __init__(self, matrix_string):
self.matrix = [[int(x) for x in row.split(" ")if x.isdigit() == True] for row in matrix_string.splitlines()]
def row(self, index):
return self.matrix[index -1]
def column(self, index):
return [x[index-1] for x in self.matrix ]
@DevShasa
DevShasa / pig_latin.py
Last active February 15, 2021 20:07
write a Python function ( pig_latin ) that takes a string as input, assumed to be an English word. The function should return the translation of this word into Pig Latin.  If the word begins with a vowel (a, e, i, o, or u), add “way” to the end of the word. So “air” becomes “airway” and “eat” becomes “eatway.”  If the word begins with any othe…
def pig_latin(word):
if word[:1] in ('a', 'e', 'i', 'o', 'u'):
return word + 'way'
return word[1:] + word[:1] + 'ay'
@DevShasa
DevShasa / name_triangle.py
Created February 11, 2021 09:43
Write a program that asks the user for their name and then produces a “name triangle”: the first letter of their name, then the first two letters, then the first three, and so forth, until the entire name is written on the final line.
name = input('What is your name: ')
name_list = ''
for character in name:
name_list += character
print(name_list)
@DevShasa
DevShasa / chopNumber.py
Created February 10, 2021 09:49
Write a function that takes a float and two integers ( before and after ). The function should return a float consisting of before digits before the decimal point and after digits after. Thus, if we call the function with 1234.5678 , 2 and 3 , the return value should be 34.567 .
def chopNumber(float_number, before_int, after_int):
# Chop before
str_truncate = str(int(float_number))
str_truncate = str_truncate[-before_int:]
str_float = float(str_truncate)
# Chop after
number_dec = float_number - int(float_number)
number_dec = str(number_dec)
number_dec = number_dec[0:after_int+2]
@DevShasa
DevShasa / count_ones.py
Last active February 5, 2021 04:56
Given two numbers: 'left' and 'right' ('left' <= 'right' ) return sum of all '1' occurrences in binary representations of numbers between 'left' and 'right' (including both)
def sumOfOnes(num):
return len([i for i in bin(num) if i == "1"])
def countOnes(left, right):
return sum([sumOfOnes(i) for i in range(left, right+1)])