diff --git a/Hackathon_Sessions/Session1_LoopsLogic_Python/For_Loops_and_If_Statements.html b/Hackathon_Sessions/Session1_LoopsLogic_Python/For_Loops_and_If_Statements.html new file mode 100644 index 0000000..8872b70 --- /dev/null +++ b/Hackathon_Sessions/Session1_LoopsLogic_Python/For_Loops_and_If_Statements.html @@ -0,0 +1,1880 @@ + + + + + + + + + + + + + +Coffee and Coding Python Hackathon: Loops and Logic + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+
+
+
+
+ +
+ + + + + + + +
+ +
+Presentation Release: April 2022 | Need help? Contact the team. +
+ +
+

+ +

In this session we will be focusing on Loops and Logic.

+

There are three exercises of varying difficulty that you should try and attempt as a group.

+
+

You don’t need to complete each exercise – the important thing is to try! A big part of learning to code and refining your skills is practice through trial and error.

+
+

You can search for some of the answers to these exercises on Google but that doesn’t mean you should.

+

Use Google to understand how things work, not how to solve the entire problem. You will get the most out of these exercises practicing in your small teams, sharing knowledge, and figuring them out together.

+

Our answers will be given and shared at the end of the session. It will great to see yours as well (no pressure and only if you feel comfortable). You may even have a better solution!!

+

If you want to share your code with the group you can:

+
    +
  • Share it on our main Teams chat when we go through each exercise. See Appendix 1 for instructions on how to copy code into Teams using the Code Snippet tool.

  • +
  • If you would prefer to share anonymously – please email us your solution at

  • +
+
+
+
+

1 Challenge One – Warm Up (Beginner)

+

Your task is to make a function, which returns the sum of a sequence of integers.

+

The sequence of integers that your function will take in as arguments is defined by 3 non-negative values:

+
    +
  • a begin value

  • +
  • an end value

  • +
  • a step value (inclusive)

  • +
+

If the begin value is greater than the end value, your function should return 0.

+
+
+

1.0.1 Example 1

+
+

If your begin_value, end_value and step_value were all equal to 2, then the result would be 2.

+
# Calling the function with the following arguments
+integer_sum(begin_val = 2, end_val = 2, step_val = 2)
+
+# The sequence would be 2
+
+# The sum of that sequence would be 2
+
+
+
+

1.0.2 Example 2

+

If your begin_value = 2, end_value = 6 and step_value = 2 , then the result would be 12.

+
# Calling the function with the following arguments
+integer_sum(begin_val = 2, end_val = 6, step_val = 2)
+
+# Sequence would be 2,4,6
+
+# Sum would be 12
+
+
+
+

1.1 Your Turn

+
+

Exercise

+

Now its your turn to create the function for this exercise. If you want a hint please click the hint tab.

+
+
+
+

Hint

+

Have a look at the in built function range and sum.

+
+
+
+
+
+

2 Challenge Two - Beginner-Intermediate

+

Given a list of integers ( [5,6,6] ), find the one that appears an odd number of times ( 5 ). There will always be only one integer that appears an odd number of times in the list!

+
+
+

2.0.1 Example 1

+
+
# given the following list of integers
+intergers = [2,2,2,5,5,5,5]
+
+# Calling your function
+odd_int_count(intergers)
+
+# should return 2 (as it appears an odd number of times)
+
+
+
+

2.0.2 Example 2

+
# given the following list of integers
+intergers = [1,2,2,2,3,3,1,1,1]
+
+# Calling your function
+odd_int_count(intergers)
+
+# should return 2 (as it appears an odd number of times)
+
+
+
+

2.1 Your Turn

+
+

Exercise

+

Now its your turn to create the function for this exercise. If you want a hint please click the hint tab.

+
+
+
+

Hint

+

Have a look at the set function and the modulo operator (%)

+
+
+
+
+
+

3 Challenge Three – Intermediate-Advanced

+

Write an algorithm that takes a list and moves all of the zeros to the end of the list, preserving the order of the other elements.

+
+
+

3.0.1 Example 1

+
+
# Creating a list
+numbers = [3,4,0,5,6]
+
+# Calling the function
+move_zeros(numbers)
+
+# Should return [3,4,5,6,0]
+
+
+
+

3.0.2 Example 2

+
# Creating a list
+numbers = [0,5,0,8,9,0,4,9,0]
+
+# Calling the function
+move_zeros(numbers)
+
+# should return [5,8,9,4,9,0,0,0,0]
+
+
+
+

3.1 Your Turn

+
+

Exercise

+

Now its your turn to create the function for this exercise. If you want a hint please click the hint tab.

+
+
+
+

Hint

+

Have a look at try and except

+
+
+
+
+
+

4 Appendix

+

Appendix - Instructions for submitting code via Teams:

+
+
    +
  • In the comments section of Teams click on the A symbol (circled red below).
  • +
+

+
+
    +
  • Click on the symbol.
  • +
+

+
+
    +
  • In the menu bar click on </> Code snippet.
  • +
+

+
+
    +
  • Add your function to the section and click insert when you are ready.
  • +
+

+
+ +
+Presentation Release: December 2021 | Need help? Contact the team. +
+
+ + + +
+
+ +
+ + + + + + + + + + + + + + + + diff --git a/Hackathon_Sessions/Session1_LoopsLogic_Python/SOLUTIONS_CnC_Loops_and_Logic.py b/Hackathon_Sessions/Session1_LoopsLogic_Python/SOLUTIONS_CnC_Loops_and_Logic.py new file mode 100644 index 0000000..5f811b7 --- /dev/null +++ b/Hackathon_Sessions/Session1_LoopsLogic_Python/SOLUTIONS_CnC_Loops_and_Logic.py @@ -0,0 +1,163 @@ +# -*- coding: utf-8 -*- +""" +Coffee and Coding Python Exercises + +Session Focus: +For Loops and If Statements + +@authors: Craig Scott, Ian Banda +""" + +#%% +# Exercise 1 + +def interger_sum(begin_val, end_val, step_val): + ''' + Sums integers between specified values. + + Parameters + ---------- + begin_val : int + value to begin sequence. + end_val : int + value to end sequence. + step_val : int + step_val the integers steps between numbers. + + Returns + ------- + int + A sum of the integers + + ''' + # Check whether the begin value is greater than end value - return 0 if True + if begin_val > end_val: + + return 0 + + else: + # Return sum of integers + return sum(range(begin_val,end_val+1,step_val)) + + +# test integer_sum using the following tests + +begin_val = 4 +end_val = 4 +step_val = 4 + +interger_sum(begin_val, end_val, step_val) +# should return 4 + +interger_sum(3,11,2) +# should return 35 (3 + 5 + 7 + 9 + 11) + +interger_sum(1,15,5) +# should return 18 (1+6+11) + +interger_sum(9,7,2) +# should return 0 (begin_val is larger than end_val) + + +#%% +# Exercise 2 + +def odd_int_count(int_array): + ''' + Returns the integer that appears an odd number of time. + + Parameters + ---------- + int_array : list + list of integers. + + Returns + ------- + i : int + The integer that appears an odd number of times. + + ''' + + # Use set to only get the unique values in the array + unique_int_array = set(int_array) + + # cycle through the unique set of integers + for i in unique_int_array: + + # use modulo to get the remainder of the division of the number of times it appears divided by 2 + # If this is 1 - the number is 1 and we return this number + if int_array.count(i) % 2: + + return i + + +# test odd_int_count using the following tests + +odd_int_count([2,2,2,5,5,5,5]) +# should return 2 (it appears an odd number of times) + +odd_int_count([1,2,2,2,3,3,1,1,1]) +# should return 2 (it appears an odd number of times) + +odd_int_count([10,10,10,10,3,4,5,4,2,1,1,5,5,3,2,10,5]) +# should return 10 (it appears an odd number of times) + + +#%% +# Exercise 3 + +def move_zeros(int_array): + ''' + Moves zeros in an array to the end of the list. + + Parameters + ---------- + int_array : list + list of ints. + + Returns + ------- + int_array : list + A reordered list with zeros at the end. + + + ''' + + # create some values to control the while loop + still_zeros = 1 + number_of_zeros = 0 + + # While the variable still_zeros is still true + while still_zeros: + + # catch an error when there are no more zeros + try: + + # get the index of he next zero + zero_index = int_array.index(0) + # increase the number zeros counter + number_of_zeros += 1 + # use the pop function to remove that zero value + int_array.pop(zero_index)number.pop + + except ValueError: + # if there are no more zeros break the while loop by changing still zeros + # to be 0 + still_zeros = 0 + + # for the number of times there was a zero in the list + for i in range(number_of_zeros): + # use append to add a zero at the end + int_array.append(0) + + return int_array + + +move_zeros([3,4,0,5,6]) +# should return [3,4,5,6,0] + +move_zeros([11,25,0,0,1,3,4,5,11,10,29,0,3]) +# Should return [11,25,1,3,4,5,11,1,29,3,0,0,0] + +move_zeros([0,5,0,8,9,0,4,9,0]) +# should return [5,8,9,4,9,0,0,0,0] \ No newline at end of file