Programming 102 Week 2 Tasks (Python)

This blog post continues on from my previous Programming 102 tasks/challenges.

2.5 Strings As Lists

Task: “On scoreboards, sports teams are sometimes referred to using a contraction of their name, often the first three letters. Can you write some code in order to create a new list, teams_short, containing three-letter contractions of each of the teams in the list, in the same order as they appear in teams?“.

My Solution:

teams = [“WOLVES”, “OWLS”, “PANTHERS”, “BEARS”, “DRAGONS”]
teams_short = []
for team in teams:
team_name = team[:3]
teams_short.append(team_name)
print(teams_short)
102_2_5_geektechstuff
Using Python to edit strings in lists

2.8 Implement Count Function

Task: “Can you add a generic count function to this program so that it prints out the number of learners that scored top marks?“.

import random
def top_scorers(score_to_count,list_to_use):
score_number = 0
for score in list_to_use:
if score == score_to_count:
score_number +=1
return (print(“{0} learners got top marks”.format(score_number)))
scores = []
for x in range (0, 30):
scores.append(random.randint(0, 10))
print(scores)
top_scorers(10, scores) # Count function called here
102_2_8_geektechstuff
Python Count Function

2.9 Time To Share Your Code

Task: “In the previous challenge you created a function to count the number of occurrences of a number in a list. Then you extended your program to count strings or letters, or used it to find the number of scores in a range.

My solution:

import random
def list_item_counter(item_to_count,list_to_use):
occurrences= 0
for item in list_to_use:
if item == item_to_count:
occurrences +=1
return (print(“{0} appeared {1} times in the list”.format(item_to_count, occurrences)))
list_to_use = “Harry Potter”
list_item_counter(“t”, list_to_use) # Count function called here

 

Python List Counter Modified
Python List Counter Modified
102_2_9_list_item_counter_2_geektechstuff
Python List Counter Modified (2)

 

2.11 Mean, Max and Min

Task: “Choose one of the following functions: mean (average), minimum, or maximum. Think of a real-world scenario where you need to work with data and find the average, minimum or maximum of a list of values. This could be using data that you work with in school, or for a hobby or other interest”.

I decided to use Python’s built in “min” and “max” functions and then created a function to look for an average wage. There are few different types of averages (see https://www.bbc.com/bitesize/articles/z99jpbk). I looked a fictional wages to see the minimum, maximum and average wage.

102_2_11_mean_max_min_geektechstuff
Python Min, Max and an Average function
wage = [500,550,500,525,800,650,600,575,650]
min_wage = min(wage)
max_wage = max(wage)
def average(list):
items_count = 0
total = 0
for item inlist:
items_count = items_count + 1
total = total + item
avg = total / items_count
avg = int(avg)
return(print(avg))
print(min_wage)
print(max_wage)
average(wage)

One response to “Programming 102 Week 2 Tasks (Python)”

Welcome to GeekTechStuff

my home away from home and where I will be sharing my adventures in the world of technology and all things geek.

The technology subjects have varied over the years from Python code to handle ciphers and Pig Latin, to IoT sensors in Azure and Python handling Bluetooth, to Ansible and Terraform and material around DevOps.

Let’s connect