Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Channel report #87

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions chk_for_22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

def has22(lst):

"""
Test to determine if there are two adjacent 2s in a passed in list
>>> x = [0, 1, 4, 2, 2, 3]
>>> has22(x)
True
>>>
>>> y = [0, 1, 2, 3, 4, 5]
>>> has22(y)
False
>>>
>>> z = "122345"
>>> has22(z)
True
>>>
"""

# Initialize counter
cnt = 0

try:
# Run through list
for x in lst:
if str(x) == "2" and str(lst[cnt + 1]) == "2":
return True
cnt += 1
except:
return False

if __name__ == "__main__":
var = [1,2,2,3,4,2]
print(has22(var))
17 changes: 17 additions & 0 deletions count13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

def count13(lst):
"""
Sum all numbers in a passed in list that are less than 13
>>> count13([1,2,5,7,8,13,15,20])
23
>>>
"""
if not type(lst) is list:
return 0
good_lst = [x for x in lst if type(x) is int or type(x) is float]
return sum([x for x in good_lst if x < 13])

if __name__ == "__main__":
lst = [1,2,5,7,8,13,15,20]
print(count13(lst))

23 changes: 23 additions & 0 deletions count_evens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

def count_evens(lst):
"""
Determines the number of even integers in a passed in list of integers
Example: count_evens([1,2,3,4]) would return 2
>>> count_evens([1,2,3,4])
2
>>>
"""
cnt = 0
if not type(lst) is list:
return 0
else:
for x in lst:
if not type(x) is int:
pass
else:
if x % 2 == 0:
cnt += 1
return cnt

if __name__ == "__main__":
print(count_evens([2,2,8,4]))
72 changes: 72 additions & 0 deletions driver_dispatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# $Id: play03.py,v 1.3 2017/02/04 20:32:31 larry Exp larry $

# Dictionary | key = int of driver ID | val = list of dispatcher ID
dis_dr_id = {'44': None,'15': [5], '13': [5], '2': [5], '26': [5], '39': [5],
'42': [5], '29': [5], '17': [5], '41': [5], '22': [5], '27': [5],
'24': [5], '18': [5], '21': [5], '36': [5], '14': [5], '19': [5],
'4': [5], '9': [5], '37': [5], '32': [5], '3': [5], '46': None,
'34': [5], '35': [5], '40': [5], '7': [5], '8': [5], '5': [5],
'33': [5], '45': None, '12': [5], '6': [5], '38': [5], '10': [5],
'31': [5], '1': [3, 5], '23': [5], '25': [5], '30': [5], '20': [5],
'43': None, '16': [5], '28': [5], '11': [5]}

def find_dispatcher (dr_id, co_id):
"""
Find dispatcher function
- Arguments are driver ID & co-driver ID
- Returns dispatcher common to both or None if not found
Example ...
dis_dr_id = {"1":[5],"2":[5]}
find_dispatcher(1,2) returns [5]
"""

# Highest possible driver number
top_num = 100
tst_lst = [x for x in range(1, top_num + 1)]

# Various error checks
# Does database exist
if not 'dis_dr_id' in globals():
return "Database does not exist!"
# Test if driver & co-driver ID are integers
elif not type(dr_id) is int or not type(co_id) is int:
return "Driver ID & co-driver ID must be integers!"
# Test values of driver & co-driver IDs
elif not dr_id in tst_lst or not co_id in tst_lst:
return "Driver ID & co-driver ID must be between 1 and " + str(top_num)
# Check if driver in database
elif not str(dr_id) in dis_dr_id:
return "Driver ID not in database."
# Check if co-driver in database
elif not str(co_id) in dis_dr_id:
return "Co-driver ID not in database."

# Driver, list of lists
dr_lst = []

# Co-driver, list of lists
co_lst = []

# Create list of dispatchers for driver & co-driver
for x in dis_dr_id:
if x == str(dr_id) and dis_dr_id[x] != None:
dr_lst.append((dis_dr_id[x]))
if x == str(co_id) and dis_dr_id[x] != None:
co_lst.append((dis_dr_id[x]))

# Common dispatcher list
dis_lst = []

for a in dr_lst:
for b in a:
for c in co_lst:
if b in c:
dis_lst.append(b)

if len(dis_lst) > 0:
return list(set(dis_lst))
else:
return None

if __name__ == "__main__":
print(find_dispatcher(1, 2))
34 changes: 34 additions & 0 deletions fizzbizz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@


def fizzbizz(lst):
"""
Simple error checking
Takes a list as an argument
Numbers that are divisable by 3 are replaced by FIZZ
Numbers that are divisable by 5 are replaced by BIZZ
Numbers that are divisable by 3 & 5 are replaced by FIZZBIZZ
>>> fizzbizz([x for x in range(1,16)])
[1, 2, 'FIZZ', 4, 'BIZZ', 'FIZZ', 7, 8, 'FIZZ', 'BIZZ', 11, 'FIZZ', 13, 14, 'FIZZBIZZ']
"""
# Test for a valid lst
if not type(lst) is list:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isinstance is a nice feature to use here. Example:
if not isinstance(lst, list):

The advantage of isinstance is that you can use it also with classes or tuples that you make along with types. for example:

class Circle():
    pass

my_circle = Circle()
if isinstance(my_circle, Circle)
    print('its a circle')
else:
    print("not a circle")

return None
# Replace divisable by 3 & 5
for x in lst:
if x % 3 == 0 and x % 5 == 0:
lst[x-1] = "FIZZBIZZ"
# Replace divisable by 3
for x in lst:
if type(x) is int:
if x % 3 == 0:
lst[x-1] = "FIZZ"
# Replace divisable by 5
for x in lst:
if type(x) is int:
if x % 5 == 0:
lst[x-1] = "BIZZ"

return lst

if __name__ == "__main__":
print(fizzbizz([x for x in range(1,101)]))
51 changes: 51 additions & 0 deletions grid_maker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

import sys

class Grid_Maker:
"""
Create a user defined grid with error check
Returns a formatted string
Parameters are:
- s (side character of grid)
- t (top character of grid)
- c (corner character of grid)
- h (number of side characters)
- w (number of top characters)
- b (number of boxes)
"""
# Initialize
def __init__(self, s = '|', t = '-', c = '+', h = 5, w = 5, b = 2):
self.s = s
self.t = t
self.c = c
self.h = h
self.w = w
self.b = b
Copy link
Contributor

@mdgregor mdgregor Mar 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should rename the variable names what you have in the docstring here
self.side_character_of_grid = s` and so forth

# Create grid
def draw_grid(self):
"""
Creates a user defined grid
"""
try:
if self.b == 1:
# Single box
v1 = self.c + (self.t * self.w) + self.c + "\n"
v2 = self.s + (" " * self.w) + self.s + "\n"
return v1 + (v2 * self.h) + v1[:-1]
else:
# Left corner + top
v1 = self.c + (self.t * self.w)
# Left side + middle
v2 = self.s + (" " * self.w)
# Create string representing grid
v3 = (v1 * self.b) + self.c + "\n"
for x in range(self.h):
v3 = v3 + (v2 * self.b) + self.s + "\n"
# Return string
return (v3 * self.b) + (self.b * v1) + self.c
except:
return "Please check arguments.\nThey must be 3 single characters & 3 integers."

if __name__ == "__main__":
gm = Grid_Maker()
print(gm.draw_grid())
Loading