CompSci
  • Home
  •  Unit 1
    Output Exercises
     Unit 2
    Variables Math Modules Exercises
     Unit 3
    Conditionals Exercises
     Unit 4
    Iteration / Loops Exercises
     Unit 5
    Strings Exercises
     Unit 6
    Lists Exercises
     Unit 7
    Functions Exercises
  •  Unit 1
    Java Introduction Java Syntax Errors & Coding Conventions Format Method Exercises
     Unit 2
    Variable Introduction Math / Arithmetic Operators Obtaining a Value Formatting Numeric Output Concatenation & Constants Division & Type Casting Exercises
     Unit 3
    Conditionals Exercises
     Unit 4
    while Loops for Loops Exercises
     Unit 5
    Strings Exercises
     Unit 6
    Methods Method Parameters Method Return Types Exercises
  • Muddy City
  • Github Classroom
  •  Assignments
    Add an Assignment Edit Assignment Search Program Code
     Forum
    Ask a Question Search the Forum
     New Account
    New Account


Account Mgmt
Reset Password

Python - Procedures


Exercises

Fix Me

Difficulty: Medium

Title: Fix Me

Description:
Write a function called that returns an array that contains exactly the same numbers as the passed array, but rearranged so that every 9 is immediately followed by a 5. Do not move the 9's, but every other number may move. The array contains the same number of 9's and 5's, every 9 has a number after it that is not a 9, and a 9 appears in the array before any 5.

  • Create a function called: fixMe
  • The fixMe function has a parameter of the original array.
  • The fixMe function returns the corrected array.
  • When a 5 is moved adjust the numbers to the right.
  • To test your code you will just pass the original array and print out the returned array.

Output:
fixMe([1, 9, 1, 5])

[1, 9, 5, 1]


fixMe([1, 9, 1, 5, 2, 9, 1, 5])

[1, 9, 5, 1, 2, 9, 5, 1]
fixMe([9, 2, 2, 5])

[9, 5, 2, 2]


Only These Characters

Difficulty: Medium

Title: Only These Characters

Description:
Write a function that returns true if all elements in an array are one of two numbers specified by the user.

  • Prompt the user for an array of 5 numbers.
  • Prompt the user for two numbers to look for in the array.
  • Write a function called only that has the following parameters: the array, first number to find, second number to find.
  • Return True If every element in the array is one of these numbers, else return False.
  • Output the value returned.

Output:
Number: 1
Number: 4
Number: 1
Number: 4
Number: 1
Number to Find: 2
Number to Find: 1

False


Number: 1
Number: 2
Number: 1
Number: 2
Number: 2
Number to Find: 1
Number to Find: 2

True


Mirror Me

Difficulty: Medium

Title: Mirror Me

Description:
For this program a "mirror" in an array is a group of contiguous elements such that somewhere in the array, the same group appears in reverse order. For example, the largest mirror section in {1, 2, 3, 8, 9, 3, 2, 1} is length 3 (the {1, 2, 3} part).

  • Create a function called: mirrorMe
  • The mirrorMe function has a parameter, which is an array.
  • Return an integer of the size of the largest mirror section found in the given array.
  • Print out the integer value returned.

Output:
mirrorMe([1, 2, 3, 8, 9, 3, 2, 1])

3


mirrorMe([7, 1, 2, 9, 7, 2, 1])

2



Computer Science