0% found this document useful (0 votes)
7 views

20 page Code Snippets (1)

The document provides guidelines for a coding competition, emphasizing that programs must produce no output other than the requested answer and should not include prompts in input statements. It includes examples of input handling, list comprehensions, string and list functions, and sample problems with solutions. Appendices offer additional resources for string and list manipulation techniques.

Uploaded by

areen.sengupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

20 page Code Snippets (1)

The document provides guidelines for a coding competition, emphasizing that programs must produce no output other than the requested answer and should not include prompts in input statements. It includes examples of input handling, list comprehensions, string and list functions, and sample problems with solutions. Appendices offer additional resources for string and list manipulation techniques.

Uploaded by

areen.sengupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Perse Coding Team Challenge Hint Sheet Overview

This title page gives reminders on input. The following pages are mostly
extracted from the excellent CodingClub cards which can also be purchased
in pocket book format here: http://codingclub.co.uk/codecards/

For this competition, your program must produce no output other than the
requested answer. For inputs then, you must use them without any
prompts e.g.

Example 1: two numbers on different lines:

num1 = int(input())
num2 = int(input())

Example 2: three numbers separated by a space on the same line:

nums = input().split()
num1 = int(nums[0])
num2 = int(nums[1])
num3 = int(nums[2])

or if you’re prefer to use this idea:


nums = [int(i) for i in input().split()]
print(nums[0]+nums[1]+nums[2])
Appendices:
1) List Comprehensions
2) More useful string and list functions
3) Example Programs
4) SPARE FOR YOUR NOTES
5) SPARE FOR YOUR NOTES
WARNING: don’t use any
prompts in your input
statements because of the auto-
marking i.e. input() only.
SEE the appendices
for some more useful
string functions.
tkinter and turtle won’t be used
in our competition but importing
modules can provide additional
good functionality.
SEE the appendices
for some more useful
list functions.
APPENDIX 1: List Comprehensions

A very useful way of processing all items in a list in simple


notation.

Example:

>>> nums = [1,2,3,4]


>>> newNums = [2*x + 1 for x in nums]
>>> print(newNums)
[3, 5, 7, 9]

In this example the strings in the list need converting to


numbers before we add them so we use the int function
within the list comprehension.

>>> textNums = ["1","2","3","4"]


>>> total = sum([int(x) for x in textNums])
>>> print(total)
10

You can also use them to filter items into a new list:
>>> nums = [1,2,3,4,5,6,7,8]
>>> odds = [int(x) for x in nums if x % 2 == 1]
>>> print(odds)
[1,3,5,7]
APPENDIX 2: More useful…
string functions
Reversing a string or extracting parts of it…

>>> A = "olleH“ >>> A = "Chat“


>>> B = A[::-1] >>> B = A[0] + A[2:]
>>> print(B) >>> print(B)
Hello Cat

Iterating through letters in a string…

>>> for x in "dog":


>>> print(x)
d
o
g

list functions
Summarising or sorting a list…
>>> A = [3,1,5,2,4]
>>> print(sum(A)) >>> A = [3,1,5,2,4]
>>> B = sorted(A)
15 >>> print(B)
>>> print(max(A) + min(A)) [1,2,3,4,5]
6

Creating a 2-D list starting with zeroes

>>> nums = [[0]*3 for _ in range(4)]


>>> nums[0][0] = 1 # set top left corner to 1
>>> nums[-1][-1] = 9 # set bottem left to 9
>>> print(nums)
[[1, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 9]]
EXAMPLE PROGRAMS
These simple programs show one way in which you might solve the following
problems:

PROBLEM A: “Input a single line containing 5 whole numbers which are each
separated by a space. Output the range of the numbers which is defined to be
the smallest number subtracted from the largest number.”

numText = input().split()
nums = [int(i) for i in numText]
sortedNums = sorted(nums)
range = sortedNums[4] - sortedNums[0]
print(range)

PROBLEM B: “Always starting with the number 1, we are going to continue to


double and add three. The sequence will start 1, 5, 13, 27, … Input a single
whole number to tell you which term of this sequence to output. For example if
the input is 3 then the output is 13 because it is the third term.”

num = int(input())
start = 1
for i in range(num-1):
start = start * 2 + 3
print(start)

PROBLEM C: “Input a whole number. If it is a three digit number whether


positive or negative (e.g. 107 or -547) and if it is divisible by 7 then output
WHIZZY otherwise output BLURP.
num = int (input())
if (num >= 100 and num <= 999) or (num <= -100 and num >= -999):
if num % 7 == 0:
print("WHIZZY")
else:
print("BLURP")
else:
print("BLURP")
SPARE NOTES PAGE 1
SPARE NOTES PAGE 2

You might also like