Purdue University - Department of Computer Science - CS Ed Week Programming Challenge 2015

CS Ed Week Programming Challenge 2015

Purdue University's Computer Science K-12 Outreach program held its third high school programming challenge during the week of December 7th-13th, 2015. The week was chosen in support of the events of Computer Science Education Week.

Computer Science Education Week was started as an attempt to help raise the profile of computer science in K-12 classrooms. The dates in December were selected in order to honor the birthday of Grace Murray Hopper, the woman who first developed a high-level programming language, as well as a compiler for converting code written in that language to instructions that a computer processor can understand.

The programming challenge offered three programming problems for students to solve during the week. These problems were designed to challenge students' abilities to think algorithmically and write solutions that address the four tenets of software design. These include a program's correctness, design, style and efficiency.

Listed below are the solutions for each of the winners from this year's competition along with their names, schools and sponsoring teacher. Each winning student will receive a Purdue Computer Science t-shirt and a certificate for their accomplishment.

Problem 1: Purdoogle

Purdue alumnus Carla Colors (class of ’91) loves the official colors associated with Purdue University, black and old gold. Carla is working on a new index for the Internet that she is calling Purdoogle. When you search the web using Purdoogle, the results returned are ranked based on their color schemes. Those sites with a primary color close to old gold will be placed at the top of the list, and those far from old gold will be placed at the bottom. To calculate the old gold rank, take the hexadecimal color string as input and determine the total distance of the red, green and blue elements from the values in old gold (#B1810B). 

Best solution by Stephen Easter from Bloomington South High School in Bloomington, IN. Sponsored by Mr. Seth Pizzo. Solution written in Python.

def oldgoldrank(s):
r1, g1, b1 = [177, 129, 11]
s = s.replace('#','')
r2, g2, b2 = tuple(ord(c) for c in s.decode('hex'))
return abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2)

Other solutions: Joe Laucella (Bloomington South HS - Bloomington, IN); Kevin Ian (Bloomington South HS - Bloomington, IN); Brendan Moore (Bloomington South HS - Bloomington, IN); Alex Rich and Rohan Reddy (New Albany HS - New Albany, IN); Juliette Landon (Lebanon HS - Lebanon, IN).

Problem 2: Largest Digit Divisor

A number that is divisible by the sum of its individual digits is known as a Harshad number. There is no specific designation for a number that is divisible by any of its digits. Write a function that checks all possible divisors made up of consecutive digits in a number N. Return the largest such divisor, or -1 if there are no divisors contained within the number’s digits. 

Best solution by Taylor Hendricks, Keith Jackson, and Frank Lam from New Albany HS in New Albany, IN. Sponsored by Ms. Mindy Johns. Solution written in Java.

import java.util.ArrayList;
import java.util.Scanner;

public class PurdueProblemTwo
{
    public static void main(String[] args)
    {
         int origNum;
         ArrayList combos = new ArrayList();
         origNum = enterNumber();
         combos = findPossibleCombinations(origNum);
         getAnswer(combos, origNum);
    }

    private static int enterNumber()
    {
         Scanner scan = new Scanner(System.in);
         System.out.println("Enter Number");
         int num = scan.nextInt();
         return num;
    }

    //puts all consecutive combinations into an arraylist
    private static ArrayList findPossibleCombinations(int num)
    {
         String newString;
         int splitnums;
         ArrayList splitnumlist = new ArrayList();

         String numString = Integer.toString(num);

         for (int x = 1 ; x < numString.length(); x++)
         {
               for (int i = 0 ; i < numString.length(); i++)
               {
                     if ((i + x) <= numString.length())
                     {
                           newString = numString.substring(i, i+x);
                           splitnums = Integer.parseInt(newString);

                           if((splitnums != 1 ) && (splitnums != 0))
                                splitnumlist.add(splitnums);
                     }
               }
         }

         return splitnumlist;
    }

    //returns largest possible answer
    private static void getAnswer(ArrayList splitnumlist, int num)
    {
         ArrayList divisorlist = new ArrayList();

         for (int x = 0; x < splitnumlist.size(); x++)
         {
               if(num % splitnumlist.get(x) == 0)
                    divisorlist.add(splitnumlist.get(x));
         }

         if(divisorlist.size() == 0)
               System.out.println("-1");

         if(divisorlist.size() > 0)
         {
               int max = divisorlist.get(0);

               for(int x = 1; x < divisorlist.size(); x++)
               {
                    if(divisorlist.get(x) > max)
                    max = divisorlist.get(x);
               }

               System.out.println("The largest divisor is " + max);
         }
    }
}

Other solutions: Neil Jain and Jacob Prince (New Albany HS - New Albany, IN); Brendan Moore (Bloomington South HS - Bloomington, IN).

Problem 3: S. Pelunker

The great cave explorer, Steve Pelunker, has always relied heavily on his compass to get him out of any unknown recess. To aid him in the modern age, he would like to use an app that tells him how far he is from his starting position based on simple compass input. Whenever he reaches a dead end, he inputs the direction he has headed as a single character from the set {N, W, E, S}, followed by the number of feet he traveled. Write the function that calculates the absolute distance from his starting location to his current location in feet (rounded to two decimals). 

Best solution #1 by Brendan Moore from Bloomington South High School in Bloomington, IN. Sponsored by Mr. Seth Pizzo. Solution written in Python.

import math
import re

def trigsolve(y, x):
    """Finds the hypotenuse of a right triangle given the two legs."""
    distance = math.sqrt(y**2 + x**2)
    return distance
    
def caveDistance(travel_path):
    """Finds the distance from the starting location given the travel path.
       Syntax: [Direction][Distance][Direction][Distance]...
       [Direction] is either 'N', 'S', 'E', or 'W'.
       [Distance] is any positive number.
       Example: 'N46S5E24W10'
    """
    y = 0
    x = 0
    
    # A regular expression that separates the given string into smaller direction-distance pairs.
    cave_regex = re.compile(r'''
        \D                       # Any non-numeral character.
        \d+                      # One or more numerals.
        \.?                      # An optional dot.
        \d*                      # Zero or more numerals.
        ''', re.VERBOSE)
    
    directions = cave_regex.findall(travel_path)    # Creates a list of all those instances.
    for direction in directions:
        if 'N' in direction:
            ndir = direction.lstrip('N')
            y += float(ndir)
        elif 'S' in direction:
            sdir = direction.lstrip('S')
            y -= float(sdir)
        elif 'E' in direction:
            edir = direction.lstrip('E')
            x += float(edir)
        elif 'W' in direction:
            wdir = direction.lstrip('W')
            x -= float(wdir)
    return round(trigsolve(y, x), 2)

Other solutions: Stephen Easter (Bloomington South HS - Bloomington, IN); Taylor Hendricks, Keith Jackson, and Frank Lam (New Albany HS - New Albany, IN); Kirston Chin and Michelle Flora (New Albany HS - New Albany, IN); Neel Jain and Jacob Prince (New Albany HS - New Albany, IN).

Last Updated: Apr 13, 2017 5:18 PM

Department of Computer Science, 305 N. University Street, West Lafayette, IN 47907

Phone:(765) 494-6010 • Fax: (765) 494-0739

Copyright © 2016 Purdue University | An equal access/equal opportunity university | Copyright Complaints

Trouble with this page? Disability-related accessibility issue? Please contact the College of Science.