Python Practice: Intermediate 1

Python Practice: Intermediate 1

12/9/2017


Introduction

Here is the first intermidiate level of python practice problems. If you're not familiar with the format, I'd encourage you to go checkout the intial post, which contains some information on the series.

I decided to mix it up a bit with this one. All of these exercises will build off of one another, and they're all focused around working with functions and lists. Since you will be writing functions, I'll provide the example function calls and sample output, and you write your function to match that. The first question should be pretty simple, and then they will build off of that and get harder as they go on.


Let's get started!


1) Generate list of random ints

Problem Statement

Write a function called generate_list that takes 1 parameter, list_length, and returns an list of list_length random integers between 1 and 100. Assume list_length is always greater than 0.

Techniques Practiced

for loops functions lists


Sample Output

random list 1: [99, 28, 80, 25]
random list 2: [66, 53, 42, 86, 16, 43, 90, 50, 81, 24]

Starter code

if __name__ == '__main__':
  print("random list 1: {}".format(generate_list(4)))
  print("random list 2: {}".format(generate_list(10)))

Solution

import random


def generate_list(list_length):
    random_list = []
    for i in range(list_length):
        random_list.append(random.randint(1, 100))

    return random_list

if __name__ == '__main__':
    print("random list 1: {}".format(generate_list(4)))
    print("random list 2: {}".format(generate_list(10)))
      


2) Generate list of lists

Problem Statement

Write a function called generate_list_of_lists, that takes two parameters, parent_list_length and list_length.

  • parent_list_length = the number of inner lists to have in your parent list
  • list_length = the length of your inner lists.
The function should return one list that contains multiple inner lists of list_length random integers between 1 and 100. You should utilize the generate_list function from exercise 1.

Techniques Practiced

for loops functions lists


Sample Output

list of lists 1: [[96, 81, 49, 40], [25, 57, 87, 56]]
list of lists 2: [[25, 2], [79, 37], [45, 46], [25, 32], [7, 8]]

Starter code

if __name__ == '__main__':
  print("list of lists 1: {}".format(generate_list_of_lists(2, 4)))
  print("list of lists 2: {}".format(generate_list_of_lists(5, 2)))

Solution

import random


def generate_list(list_length):
    random_list = []
    for i in range(list_length):
        random_list.append(random.randint(1, 100))

    return random_list

def generate_list_of_lists(parent_list_length, list_length):
    parent_list = []
    for i in range(parent_list_length):
        parent_list.append(generate_list(list_length))

    return parent_list

if __name__ == '__main__':
    print("list of lists 1: {}".format(generate_list_of_lists(2, 4)))
    print("list of lists 2: {}".format(generate_list_of_lists(5, 2)))

      


3) Sum inner lists

Problem Statement

Write a function, sum_inner_lists, that takes 1 parameter, parent_list, which is a list of lists (as generated in exercise 2). The function should return a single list that contains the sum of each inner list.

Techniques Practiced

for loops functions lists


Sample Output

parent_list: [[12, 20, 94, 9], [76, 16, 40, 2], [44, 35, 76, 69]]
summed_list: [135, 134, 224]

Starter Code

if __name__ == '__main__':
  parent_list = generate_list_of_lists(3, 4)
  print("parent_list: {}".format(parent_list))
  print("summed_list: {}".format(sum_inner_lists(parent_list)))

Solution

import random


def generate_list(list_length):
    random_list = []
    for i in range(list_length):
        random_list.append(random.randint(1, 100))

    return random_list

def generate_list_of_lists(parent_list_length, list_length):
    parent_list = []
    for i in range(parent_list_length):
        parent_list.append(generate_list(list_length))

    return parent_list


def sum_inner_lists(parent_list):
    summed_list = []
    for inner_list in parent_list:
        summed_list.append(sum(inner_list))

    return summed_list

if __name__ == '__main__':
    parent_list = generate_list_of_lists(3, 4)
    print("parent_list: {}".format(parent_list))
    print("summed_list: {}".format(sum_inner_lists(parent_list)))
        
      


4) Sum inner lists by index

Problem Statement

This is a fun one. Write a function, sum_inner_lists_by_index, which takes in one parameter, parent_list, which is a list of lists. Have your function sum the first index of each inner list together, the second index of each inner list together, for n indices. The return should be a list that is the sum of each index. Ex:

[sum(ind 0), sum(ind 1), ..., sum(ind n)]

I took the approach of first rearranging my lists, then using the sum function from problem 3. This will be reflected in my sample output, but you can solve it however you'd like.

Techniques Practiced

for loops functions lists


Sample Output

Example 1:

parent_list: [[43, 91], [3, 99]]
rearranged list: [[43, 3], [91, 99]]
final summed list by index: [46, 190]

Example 2:

parent_list: [[17, 15, 53, 61], [47, 96, 34, 63], [77, 74, 58, 70]]
rearranged list: [[17, 47, 77], [15, 96, 74], [53, 34, 58], [61, 63, 70]]
final summed list by index: [141, 185, 145, 194]

Starter Code

if __name__ == '__main__':
  parent_list = generate_list_of_lists(3, 4)
  print("parent_list: {}".format(parent_list))
  
  rearranged_parent_list = rearrange_inner_list_by_index(parent_list)
  print("rearranged list: {}".format(rearranged_parent_list))

  sum_of_rearranged_list = sum_nested_lists(rearranged_parent_list)
  print("final summed list by index: {}".format(sum_of_rearranged_list))

Solution

import random
        
        
def generate_list(list_length):
    random_list = []
    for i in range(list_length):
        random_list.append(random.randint(1, 100))

    return random_list


def generate_list_of_lists(parent_list_length, list_length):
    parent_list = []
    for i in range(parent_list_length):
        parent_list.append(generate_list(list_length))

    return parent_list


def sum_nested_lists(parent_list):
    summed_list = []
    for inner_list in parent_list:
        summed_list.append(sum(inner_list))

    return summed_list


def rearrange_inner_list_by_index(parent_list):
    # First, generate empty return list structure. 
    # It should have as many inner lists as elements in each list
    return_list = [[] for element in parent_list[0]]

    for inner_list in parent_list:
        for element in enumerate(inner_list):
            return_list[element[0]].insert(parent_list.index(inner_list), element[1])

    return return_list

if __name__ == '__main__':
    parent_list = generate_list_of_lists(3, 4)
    print("parent_list: {}".format(parent_list))
    
    rearranged_parent_list = rearrange_inner_list_by_index(parent_list)
    print("rearranged list: {}".format(rearranged_parent_list))

    sum_of_rearranged_list = sum_nested_lists(rearranged_parent_list)
    print("final summed list by index: {}".format(sum_of_rearranged_list))
      


I hope you enjoyed these 4 practice problems on functions and lists. Problem number 4 took me a pretty long time to solve, it wasn't an easy one!

Let me know if you thought these were too easy or too hard for an intermediate-level developer. I hope to have some more practice problems out in the next few weeks!


Check out more posts in the Python category!