CHALLENGES

Practice your skills with a few coding challenges

Go from easy problems to more difficult problems that test and hone your programming knowledge with a few challenges. Each challenge will test one or more aspects of programming knowledge and there are hints/solutions to each problem with explanations. But remember, there may be more than one solution to each problem. These challenges are primarily designed for Ophidian users but if you would like to test your skills in other programming languages then feel free to try them out as well.


Challenge set #1: BEGINNER

Covers the fundamental skills such as writing to the output and variables.

Instructions: Write the following to the output

Good morning

Good afternoon

Good night

Hint:

Use the WRITE() method

Sample Solution:

WRITE("Good morning")

WRITE("Good afternoon")

WRITE("Good night")

2. greetings

Instructions: Create a variable called "name" to store the input of a user's name. Then write out a greeting to the output that says "Hello" to the user. 

Sample Output:

Hello, Bob

Hint:

Use the INPUT() method

Sample Solution:

VAR name = INPUT() # Takes the input of the user

WRITE("Hello, " + name) # Adds the user's name to the string

3. favorite number

Instructions: Create a variable called 'number' to store the input of a user's favorite number. Make sure the user can ONLY input a number and then write to the output "Your favorite number is: " and the user's number

Sample Output:

Your favorite number is: 4

Hint:

Use the TO_STRING() and INPUT_NUMBER()method

Sample Solution:

VAR number = INPUT_NUMBER() # Takes the number input of a user and only allows a user to put a number

WRITE("Your favorite number is: " + TO_STRING(number)) # convert the number to a string to allow adding  

4. Distance between two numbers

Instructions: Create a program that allows the user to input a number into two values and then find the positive distance between those values.

Examples:

INPUT: 2, 3

OUTPUT: 1


INPUT: 5, 35

OUTPUT: 30

Hint:

Use the ABSOLUTE_VAL()method

Sample Solution:

VAR x = INPUT_NUMBER()

VAR y = INPUT_NUMBER()

WRITE(ABSOLUTE_VAL(x-y))

5. FINDING THE AVERAGE of 3 numbers

Instructions: Create a program that allows the user to set 3 values and then find the average of those values rounded to 2 decimal places

Examples:

INPUT: 3, 5, 6

OUTPUT: 4.67


INPUT: 2.3, 3.5, 4.8

OUTPUT: 3.53

Hint:

Use the ROUND()method

Sample Solution:

# Gets the number inputs

VAR x = INPUT_NUMBER()

VAR y = INPUT_NUMBER()

VAR z = INPUT_NUMBER()


# Create a variable to store the average

VAR avg = (x + y + z)/3 


# Use the ROUND() method to round the average to 2 decimal points

WRITE(ROUND(avg, 2))

Challenge set #2: Novice

Explores conditional statements and loops

Instructions: Write a program that outputs how hot it is depending on the temperature

Example:

INPUT: 86

OUTPUT: Hot


INPUT: 78

OUTPUT: Just Right


INPUT: 46

OUTPUT: Cold

Hint:

Use an IF statement

Sample Solution:

#  Create a variable to store the temperature
VAR temp = INPUT_NUMBER()


# Check if temperature meets certain conditions

IF temp >= 85 DO

    WRITE("Hot")

ELSEIF temp >= 70 DO

    WRITE("Just right")

ELSE

# If temperature doesn't meet the other conditions, write out "Cold"

    WRITE("Cold")

END




2. guess the password

Instructions: Create a game that lets the user guess a password and if write out a message depending on whether or not the guess was correct or not. If the password is guessed, end the game

Example:

PASSWORD: "password"

INPUT: "guess1"

OUTPUT: Incorrect, try again

INPUT: "guess2"

OUTPUT: Incorrect, try again

INPUT: "password"
OUTPUT: Correct!




Hint:

Use a WHILE loop and store the input in a variable 

Sample Solution:

# Initialize variables to store the password and global input

VAR password = "password"

VAR guess = ""


# Keep the game running if password isn't guessed

WHILE guess != password DO

    # Store the input in 'guess'

    VAR guess = INPUT()

    # Write an incorrect message if loop condition is unfulfilled

    WRITE("Incorrect, try again")

END

# When loop is broken, that means the password is correct

WRITE("Correct!")




Challenge set #X: KNOWLEDGEABLE

Once you have a basic grasp on how coding works, take on these challenges to see if you can do them 

Instructions: Write a function that reverses a string. The input is given as a string object. 

Examples:

INPUT: hello

OUTPUT: elleh


INPUT: Racecar

OUTPUT: racecaR

Hint:

Use a for loop and get the character positions

Sample Solution:

# Declare the function
CREATE FUNCTION reverse(s)


    # Create an empty string

    VAR str = ""

    # initialize 'i'

    VAR i = 0

    

    # iterate through the input

    FOR i FROM 0 TO LENGTH_OF(s) DO

# retrieve associated chars from index value

        VAR letter = INDEX_OF(s, i)


# append each letter to the empty string

        VAR str = letter + str

    END

    RETURN str

END



VAR string = "Funny"


# formatting the output

WRITE("The original string is: " + string)


WRITE("The reversed string is: " + reverse(string))