CS 112 Programming Assignment 5
1 | Loops |
The purpose of this assignment is to practice using loops and basic lists effectively.
See the “assignment basics” file for more detailed information about getting assistance, running the
test file, grading, commenting, and many other extremely important things. Each assignment is
governed by the rules in that document:
https://piazza.com/class_profile/get_resource/k4wblafqtoj2cx/k5x3ennufm34cg
Needed file, the tester for this assignment:
https://piazza.com/class_profile/get_resource/k4wblafqtoj2cx/k75awsh1vap1yh
Background
Loop statements allow us to run the same block of code repeatedly, with the chance to use different
values for variables each time as the accumulated effects pile up. Lists are sequences that can be
inspected value-by-value and modified at will. In this assignment we will use loops to perform
calculations over one-dimensional lists (in future assignments we will work with multi-dimensional
lists too). However, working with one-dimensional lists doesn’t necessarily mean that a single
while/for loop is always enough, sometimes a repeated iteration over a list might require a (double
or even triple) nested loop.
Guidelines
You are not allowed to import anything.
You are not allowed to use sets, dictionaries
You are not allowed to use anything that hasn’t been covered in class, including the list
comprehension construct (if you don’t know it, don’t worry, it’s impossible to use it by accident!)
From built-in functions, you are allowed to call only range() , len() , abs() , int() , str()
From list methods, you are allowed to use only .append(). Please do not ask on piazza
whether you can use .sort(), .index(), .count() etc.
You are not allowed to hard-code exhaustive if-elif-elif statements to handle all possible
list lengths. Your code should work with any length of list even if it were larger than 13.
You can only use slicing if it doesn’t bypass a restriction. For example, you can’t use slicing to
reverse a list since .reverse() is not allowed either.
When a specific test case isn’t working, plug your code into the visualizer to watch what
the code does, which lines run, which branches are taken.
Insert comments in the lines you deem necessary
Testing
In this assignment testing will be done a bit different than before:
- You will start working on the assignment without a tester, and you will do your own testing based on
the examples we provide in this document as well as other examples you can come up with. Later in
the week we will provide an actual tester but don’t wait for it in order to start working on the
assignment as there won’t be enough time to complete it before the deadline. The purpose of the
delayed release of the tester is for you to put more emphasis/effort on writing logically correct
programs instead of trying to pass certain tests only.
- When we post the tester, we’re going to omit some of the test cases that will be used for grading.
This means that there might be errors in your code even if the tester is giving you no errors. You must
do your own checks to make sure that you haven’t missed anything and your code is correct. You do
not need to modify the tester, just test on your own any way you like. Again, the goal is to help you put
more focus on writing logically correct programs instead of trying to pass certain tests only.
Grading Rubric
Submitted correctly:
Code is well commented:
Tester calculations correct:
Unknown test cases
TOTAL:
2
8
70
20
100
# see assignment basics file for file requirements!
# see assignment basics file for how to comment!
# see assignment basics file for how to test!
# test cases that are omitted from the tester
Note : If your code does not run and crashes due to errors, it will receive zero points. Turning in
running code is essential.
Scenario
You’re working for the same software company that you did in PA3 and your manager asks you to
write a new version of the poker application, only this time it’s going to be more powerful and
customizable for more variants of the poker game. More specifically, it should work with any number
of cards, not just 3 as before but with larger numbers too (e.g. 4 , 5 , 7 , etc.). For the purposes of this
assignment we will assume that the hand can have any length from 3 up to 13 included. We will also
assume that the following hands and ranks apply to all variants.
Rank Description
Straight flush ALL suited cards in sequence
N of a kind N cards of same rank ( N will be either 3 or 4)
Straight ALL cards in sequence
Flush ALL suited cards
N pairs N different pairs of two cards of same rank ( N will be either 1 or 2)
Table 1
For each of these hands you will implement a function that takes a list of numbers that represent the
cards (and for some functions a second parameter too) and returns some value according to the
specification. We will use the Table 2 mapping to represent the standard 52-card deck with integers.
Ace will always rank low (i.e. be the lowest card when considering a sequence for straight).
1 2 3 4 5 6 7 8 9 10 11 12 13
Clubs
1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 |
Diamonds
1 | 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Hearts
1 | 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Spades
1 | 40 41 42 43 44 45 46 47 48 49 50 51 52 |
Table 2
Assumptions
You may assume that:
- The types of the values that are sent to the functions are the proper ones (card_list is a list
of integers not floats, etc.), you don’t have to validate them.
- The functions are going to be called with usable values (e.g. card_list doesn’t contain
negative numbers, etc.), you don’t have to validate them.
- All card numbers that are passed into a function are guaranteed to be unique, you don’t have to
validate that either.
Functions
The signature of each function is provided below, do not make any changes to them otherwise the
tester will not work properly. The following are the functions you must implement:
straight_flush(card_list)
Description: The function checks whether a complete list of cards makes up a straight flush hand, and
only that. ( Hint : check the helper functions)
1 | Parameters: card_list (list of int) contains a list of cards represented with the mapping provided |
1 | Return value: If the hand is not a straight flush return False , otherwise return the face value of the |
1 | Examples: |
n_of_a_kind(card_list, cardinality)
1 | Description: Based on its second parameter, cardinality , the function checks whether the cards |
1 | Parameters: card_list (list of int) contains a list of cards represented with the mapping provided |
1 | Return value: If the hand is not an n of a kind return False , otherwise return the face value of the card |
1 | Examples: |
straight(card_list)
1 | Description: The checks whether the cards make up a straight hand, and only that. |
1 | Parameters: card_list (list of int) contains a list of cards represented with the mapping provided |
1 | Return value: If the hand is not a straight return False , otherwise return the face value of the highest |
1 | Examples: |
flush(card_list)
1 | Description: The function checks whether the cards make up a flush hand, and only that. |
1 | Parameters: card_list (list of int) contains a list of cards represented with the mapping provided |
1 | Return value: If the hand is not a flush return False , otherwise return the face value of the highest card |
1 | Examples: |
pair(card_list, num_of_pairs)
Description: Based on its second parameter, num_of_pairs , the function checks whether the cards
make up a hand of one pair or a hand of two pairs.
1 | Parameters: card_list (list of int) contains a list of cards represented with the mapping provided |
1 | Return value: If the hand is not a one/two pair return False , otherwise return the face value of the card |
1 | Examples: |
Helper functions – OPTIONAL BUT HIGHLY ENCOURAGED
If you want to avoid repeating some computations again and again, you should create your own
helper functions. It’s optional but is highly encouraged to make your life easier and your code much
simpler. Some functions that you might find extremely useful to implement are the following:
suit(card)
Description: Given a card number it returns the suit of the card as a string
value(card)
Description: Given a card number it returns the face value of the card as an int
sequence(card_list)
Description: Given a list of card numbers it checks whether the cards form a sequence or not. Hint : One
approach is to sort the whole list but it’s not easy to implement and it’s not optimal either. An
easier/better way is to exploit the fact that, for any given list, you can easily come up with the sequence
you’re looking for, and then just search for it.
same_suit(card_list)
Description: Given a list of card numbers it checks whether the cards have the same suit or not.