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 @@ + + + + +
+ + + + + + + + +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 coffee.coding@ons.gov.uk
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.
+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
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
Now its your turn to create the function for this exercise. If you want a hint please click the hint tab.
+Have a look at the in built function range and sum.
+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!
+# 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)
# 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)
Now its your turn to create the function for this exercise. If you want a hint please click the hint tab.
+Have a look at the set function and the modulo operator (%)
+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.
+# Creating a list
+numbers = [3,4,0,5,6]
+
+# Calling the function
+move_zeros(numbers)
+
+# Should return [3,4,5,6,0]
# 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]
Now its your turn to create the function for this exercise. If you want a hint please click the hint tab.
+Have a look at try and except
+Appendix - Instructions for submitting code via Teams:
+