Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part III: The Look-and-Say Sequence (5 points) In mathematics, the look-and-say

ID: 3816444 • Letter: P

Question

Part III: The Look-and-Say Sequence (5 points) In mathematics, the look-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, . . . To generate a member of the sequence from the previous member, read off the digits of the previous member, counting the number of digits in groups of the same digit. For example: • 1 is read off as “one 1” or 11 • 11 is read off as “two 1s” or 21 • 21 is read off as “one 2, then one 1” or 1211 • 1211 is read off as “one 1, one 2, then two 1s” or 111221 • 111221 is read off as “three 1s, two 2s, then one 1” or 312211 Write the function look say() that takes an integer giving which term of the look-and-say sequence we want, starting from 0, and returns a string containing that term. Term #0 is ’1’, term #1 is ’11’, term #2 is ’21’, and so on. Your function must return the correct string for any non-negative integer argument. No matter how long the numbers become, the only digits that will appear are 1, 2 and 3. Examples:

Note that the quotation marks displayed in the return values are there to emphasize that the return values are strings. You should not add quotation marks to your return values. Part IV: From Human-readable Time Intervals to Date Components (5 points) Write the function readable interval() that takes a string consisting of a human-readable time interval and converts it into a list of six integers, given in this order: [# of years, # of months, # of days, # of hours, # of minutes, # of seconds]. The input values stored within the string can be quite scrambled and consist of multiple, repeated time components (days, months, etc.), as shown in the examples below. If a time component appears more than once, the values are be added. The time units might be given in singular or plural form (e.g., “month” and “months” are both valid). There will always be at least (but not necessarily exactly) one space between the tokens (i.e., components) of the string.

Examples:

Part V: Poker Hands (5 points) Write the function poker hand() that takes a list of exactly five Card objects as an argument, analyzes the list, and returns one of the following strings that describes the hand: • ’Straight’ (five cards when rearranged will form a run of five consecutive cards) • ’Four of a kind’ (four or five cards of the same rank) • ’Three of a kind’ (exactly three cards of the same rank) • ’One pair’ (at least one pair of the same rank, but not four cards of the same rank) • ’High card’ (no cards of identical rank that also do not form a straight) The string ’High card’ is returned when no pair, triple or quadruple is found in the list of cards. The returned string may be capitalized any way you wish, but no characters (including spaces) may otherwise be added or changed.

If two pairs are present in the hand, the function should simply return ’One pair’. If a pair of one rank and a three-of-a-kind of another rank are present in the hand, the function should simply return ’Three of a kind’. The Card class that you will find in homework3.py is the same class we studied in lecture. Use it to complete this part of the assignment. You may add additional instance variables or methods to the class if you want. Examples: The numbers given to the constructors below are in the range 0 through 51, as in the examples from lecture and the textbook. You will need to use the rank() method of the Card class to extract the rank of each Card. When you run the provided homework3.py file, you will see the actual rank and suit of each card printed on the screen.

Function Call Return Value look say (3) 12 11' look-say (8) 311 312 11 131221' look-say (12) 1321 132132 11 12131221 1231 131 122 21 1311 1221 131221

Explanation / Answer

Part III:
import java.util.*;
public class LookAndSaySequence
{
   public static String Look_Say(final String number)
   {
       if (null == number || number.isEmpty())
       {
       return "";
       }
       int firstCharPosition = 0;  
       final StringBuilder stringBuilder = new StringBuilder();  
       for (int i = 0; i < number.length(); i++)  
       {  
           if (number.charAt(firstCharPosition) != number.charAt(i))  
       {  
               final String digitsFound = number.substring(firstCharPosition, i);  
               stringBuilder.append(digitsFound.length()).append(number.charAt(firstCharPosition));  
               // Set the new first char position
               firstCharPosition = i;  
       }//End of if  
       }//End of for loop
  
       //Add the leftover  
       stringBuilder.append(number.substring(firstCharPosition,number.length()).length()).append(number.charAt(firstCharPosition));  
       return stringBuilder.toString();  
   }//End of method
   //Main method
   public static void main(String ss[])
   {
       int times;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter how many terms you want?");
       times = sc.nextInt();
       //First character displayed
       String result1 = "1";
       System.out.println("Each Term: ");
       System.out.print(result1 + " ");
       for (int i = 1; i <= times; i++)      
       {      
           result1 = Look_Say(result1);      
           System.out.print(result1 + " ");      
       }//End of for loop
       System.out.println(" Final Result: " + result1);
   }//End of main method
}//End of class

Sample Run 1:

Enter how many terms you want? 3
Each Term:
1 11 21 1211

Final Result: 1211

Sample Run 2:

Enter how many terms you want? 8
Each Term:
1 11 21 1211 111221 312211 13112221 1113213211 31131211131221

Final Result: 31131211131221

Sample Run 3:

Enter how many terms you want? 12
Each Term:
1 11 21 1211 111221 312211 13112221 1113213211 31131211131221 13211311123113112211 11131221133112132113212221 3113112221232112111312211312113211 1321132132111213122112311311222113111221131221

Final Result: 1321132132111213122112311311222113111221131221

Part IV:

import java.util.*;
//Class StringOperations definition
public class StringOperations
{
   //Calculates and returns an array having year, month, day, hour, minute and second
   int [] redable_interval(String interval)
   {
       //Creates an array of size 6
       int intervals[] = new int[6];
       //Split data on space
       String value[] = interval.split(" ");
       //Loops till end of string
       for(int i = 0; i < value.length; i++)
       {
           //Check for year or years
           if(value[i].compareTo("year") == 0 || value[i].compareTo("years") == 0)
               //Add years data
               intervals[0] += Integer.parseInt(value[i-1]);
           //Check for month or months
           else if(value[i].compareTo("month") == 0 || value[i].compareTo("months") == 0)
               //Add months data
               intervals[1] += Integer.parseInt(value[i-1]);
           //Check for day or days
           else if(value[i].compareTo("day") == 0 || value[i].compareTo("days") == 0)
               //Add days data
               intervals[2] += Integer.parseInt(value[i-1]);
           //Check for hour or hours
           else if(value[i].compareTo("hour") == 0 || value[i].compareTo("hours") == 0)
               //Add hours data
               intervals[3] += Integer.parseInt(value[i-1]);
           //Check for minute or minutes
           else if(value[i].compareTo("minute") == 0 || value[i].compareTo("minutes") == 0)
               //Add minutes data
               intervals[4] += Integer.parseInt(value[i-1]);
           //Check for second or seconds
           else if(value[i].compareTo("second") == 0 || value[i].compareTo("seconds") == 0)
               //Add seconds data
               intervals[5] += Integer.parseInt(value[i-1]);          
       }//End of loop
       //Returns array
       return intervals;
   }//End of method
  
   //Main method
   public static void main(String ss[])
   {
       //Creates scanner class object to accept data
       Scanner sc = new Scanner(System.in);
       //Creates an integer array of size 6 to store the result
       int res [] = new int[6];
       String interval;
       //Creates an object of the class StringOperations
       StringOperations so = new StringOperations();
       //Accepts data
       System.out.println("Enter the data in given order [# of years # of months # of days # of hours # of minutes # of seconds]" );
       interval = sc.nextLine();
       res = so.redable_interval(interval);
       //Display the result
       System.out.println("[ " + res[0] + ", " + res[1] + ", " + res[2] + ", " + res[3] + ", " + res[4] + ", " + res[5] + " ]");
   }//End of main
}//End of class

Sample Run 1:

Enter the data in given order [# of years # of months # of days # of hours # of minutes # of seconds]

1 year 2 months 3 seconds
[ 1, 2, 0, 0, 0, 3 ]

Sample Run 2:

Enter the data in given order [# of years # of months # of days # of hours # of minutes # of seconds]
1 second 2 years 3 seconds 7 months 6 days
[ 2, 7, 6, 0, 0, 4 ]

Sample Run 3:

Enter the data in given order [# of years # of months # of days # of hours # of minutes # of seconds]
1 days 4 days 3 years 4 hours 17 minutes
[ 3, 0, 5, 4, 17, 0 ]

Sample Run 4:

Enter the data in given order [# of years # of months # of days # of hours # of minutes # of seconds]
6 hours 20 minutes 421 seconds
[ 0, 0, 0, 6, 20, 421 ]

Sample Run 5:

Enter the data in given order [# of years # of months # of days # of hours # of minutes # of seconds]
0 days 0 months 7 months 18 years
[ 18, 7, 0, 0, 0, 0 ]