Difference between revisions of "CSC111 Exercises with For-Loops: Repeating things"
(→Exercise 2) |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 10:17, 28 February 2014 (EST) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 10:17, 28 February 2014 (EST) | ||
---- | ---- | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <bluebox> | ||
+ | This page contains exercises on for-loops and short videos presenting solutions for each exercise. | ||
+ | </bluebox> | ||
<br /> | <br /> | ||
=Exercise 1= | =Exercise 1= | ||
Line 10: | Line 15: | ||
write a program that will ask the user for a number and will indicate if this number is in the array or not | write a program that will ask the user for a number and will indicate if this number is in the array or not | ||
<br /> | <br /> | ||
+ | <br /><center><videoflash>_FZEwCOyQvA</videoflash></center> | ||
+ | <br /> | ||
+ | |||
=Exercise 2= | =Exercise 2= | ||
<br /> | <br /> | ||
Line 18: | Line 26: | ||
write a program that will ask the user for a word and will indicate '''if this word is in the array''' or not | write a program that will ask the user for a word and will indicate '''if this word is in the array''' or not | ||
<br /> | <br /> | ||
− | + | <br /><center><videoflash>y1dv69bSSwk</videoflash></center> | |
+ | <br /> | ||
=Exercise 3= | =Exercise 3= | ||
<br /> | <br /> | ||
− | Same as Exercise 1, but this time the program returns the '''number of times''' the user choice appears in the array. | + | Same as Exercise 1, but this time the program returns the '''number of times''' the user's choice appears in the array. |
+ | <br /> | ||
+ | <br /><center><videoflash>1VXPBeYBiuo</videoflash></center> | ||
<br /> | <br /> | ||
=Exercise 4= | =Exercise 4= | ||
<br /> | <br /> | ||
Given the array sound, and an integer input by the user, your program will return the location of the '''first''' occurrence of the user's number. | Given the array sound, and an integer input by the user, your program will return the location of the '''first''' occurrence of the user's number. | ||
+ | <br /> | ||
+ | <br /><center><videoflash>TNfI1tO70CE</videoflash></center> | ||
<br /> | <br /> | ||
=Exercise 5= | =Exercise 5= | ||
<br /> | <br /> | ||
Given the array sound, and an integer input by the user, your program will return the location of the '''last''' occurrence of the user's number. | Given the array sound, and an integer input by the user, your program will return the location of the '''last''' occurrence of the user's number. | ||
+ | <br /> | ||
+ | |||
+ | <br /><center><videoflash>YF9dxRkY4E8</videoflash></center> | ||
+ | <!--source lang="python"> | ||
+ | sound = [1, 10, 3, 10, 3, 10, 2 ] | ||
+ | |||
+ | x = int( input( "Enter an integer: " ) ) | ||
+ | |||
+ | pos = -1 | ||
+ | for i in range( len( sound )-1, -1, -1 ): | ||
+ | if sound[i] == x: | ||
+ | pos = 1 | ||
+ | break | ||
+ | |||
+ | if pos != -1: | ||
+ | print( "Found %d at position %d" % (x, pos ) ) | ||
+ | else: | ||
+ | print( "%d not found!" %x ) | ||
+ | |||
+ | </source--> | ||
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 22:54, 28 February 2014
--D. Thiebaut (talk) 10:17, 28 February 2014 (EST)
This page contains exercises on for-loops and short videos presenting solutions for each exercise.
Exercise 1
Given the array sound
sound = [ 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 9, 1, 1, 0, 0]
write a program that will ask the user for a number and will indicate if this number is in the array or not
Exercise 2
Given the array words
words = [ "hello", "there!", "there", "will", "be", "a", "big", "snow", "storm", "on", "Monday" ]
write a program that will ask the user for a word and will indicate if this word is in the array or not
Exercise 3
Same as Exercise 1, but this time the program returns the number of times the user's choice appears in the array.
Exercise 4
Given the array sound, and an integer input by the user, your program will return the location of the first occurrence of the user's number.
Exercise 5
Given the array sound, and an integer input by the user, your program will return the location of the last occurrence of the user's number.