0% found this document useful (0 votes)
26 views6 pages

ASE Exercise 8 (Fall 2015) : Task 1 (Skill Level 0)

This document provides descriptions and solutions for 9 programming tasks of varying skill levels. The tasks involve writing methods to solve problems such as returning the closest number to 10 from two inputs, removing a character from a string at a given index, checking if an array contains the sequence 1, 2, 3, and more. Solutions are provided in Java code for each task. The document encourages attempting the tasks independently over the holiday and notes all solutions will be uploaded in January.

Uploaded by

liju101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

ASE Exercise 8 (Fall 2015) : Task 1 (Skill Level 0)

This document provides descriptions and solutions for 9 programming tasks of varying skill levels. The tasks involve writing methods to solve problems such as returning the closest number to 10 from two inputs, removing a character from a string at a given index, checking if an array contains the sequence 1, 2, 3, and more. Solutions are provided in Java code for each task. The document encourages attempting the tasks independently over the holiday and notes all solutions will be uploaded in January.

Uploaded by

liju101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ASE Exercise 8 (Fall 2015)

In this exercise we will focus on programming tasks. These tasks have different skill levels
(from 0 very easy to 10 more advanced). We have added some more advanced exercises, to
allow you to do some of these exercises during holiday time. We will upload all solutions to
the L2P at the beginning of January. Until then use the time to try to do it on your own.

Task 1 (Skill Level 0)


Implement the method public int close10(int a, int b).

Given 2 int values, return whichever value is nearest to the value 10, or return 0 in the event
of a tie.

close10(8, 13) → 8
close10(13, 8) → 8
close10(13, 7) → 0

Hint: Math.abs(n) returns the absolute value of a number.

Solution:
public int close10(int a, int b) {
int aDiff = Math.abs(a - 10);
int bDiff = Math.abs(b - 10);

if (aDiff < bDiff) {


return a;
}
if (bDiff < aDiff) {
return b;
}
return 0; // i.e. aDiff == bDiff

// Solution notes: aDiff/bDiff local vars clean the code up a bit.


// Could have "else" before the second if and the return 0.
}
http://www.ima.rwth-aachen.de

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 2

Task 2 (Skill Level 2)


Implement the method public String missingChar(String str, int n). Use existing methods of
string to solve the problem – see http://docs.oracle.com/javase/7/docs/api/java/lang/String.html.

Given a non-empty string str and an int n, return a new string where the char at index n has
been removed. The value of n will be a valid index of a char in the original string (i.e. n will be
in the range 0 to str.length()-1 inclusive).

missingChar("kitten", 1) → "ktten"
missingChar("kitten", 0) → "itten"
missingChar("kitten", 4) → "kittn"

Hint: Take a look at the method substring of string.

Solution:
public String missingChar(String str, int n) {
String front = str.substring(0, n);

// Start this substring at n+1 to omit the char.


// Can also be shortened to just str.substring(n+1)
// which goes through the end of the string.
String back = str.substring(n+1, str.length());

return front + back;


}

Task 3 (Skill Level 3)


Implement the method public boolean array123(int[] nums).

Given an array of ints, return true if the sequence 1, 2, 3 appears in the array somewhere.

array123({1, 1, 2, 3, 1}) → true


array123({1, 1, 2, 4, 1}) → false
array123({1, 1, 2, 1, 2, 3}) → true

Solution:
public boolean array123(int[] nums) {
// Note: iterate < length-2, so can use i+1 and i+2 in the loop
for (int i=0; i < (nums.length-2); i++) {
http://www.ima.rwth-aachen.de

if (nums[i]==1 && nums[i+1]==2 && nums[i+2]==3) return true;


}
return false;
}

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 3

Task 4 (Skill Level 3)


Implement the method public boolean noTriples(int[] nums).

Given an array of ints, we'll say that a triple is a value appearing 3 times in a row in the array.
Return true if the array does not contain any triples.

noTriples({1, 1, 2, 2, 1}) → true


noTriples({1, 1, 2, 2, 2, 1}) → false
noTriples({1, 1, 1, 2, 2, 2, 1}) → false

Solution:
public boolean noTriples(int[] nums) {
// Iterate < length-2, so can use i+1 and i+2 in the loop.
// Return false immediately if every seeing a triple.
for (int i=0; i < (nums.length-2); i++) {
int first = nums[i];
if (nums[i+1]==first && nums[i+2]==first) return false;
}

// If we get here ... no triples.


return true;
}

Task 5 (Skill Level 5)


Implement the method public boolean makeBricks(int small, int big, int goal).

We want to make a row of bricks that is goal inches long. We have a number of small bricks
(1 inch each) and big bricks (5 inches each). Return true if it is possible to make the goal by
choosing from the given bricks.

Note: It can be done without any loops (try to do it).

makeBricks(3, 1, 8) → true
makeBricks(3, 1, 9) → false
makeBricks(3, 2, 10) → true

Solution:
public boolean makeBricks(int small, int big, int goal) {
http://www.ima.rwth-aachen.de

final int BIG_INCH = 5;


int numberOfMatchingBigBricks = goal / BIG_INCH;
int inchesCovered = BIG_INCH * Math.min(numberOfMatchingBigBricks , big);
int numberOfNeededSmallBricks = goal - inchesCovered;
return numberOfNeededSmallBricks <= small;
}

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 4

Task 6 (Skill Level 5)


Implement the method public boolean hasOne(int n).

Given a positive int n, return true if it contains a 1 digit. Note: use % to get the rightmost
digit, and / to discard the rightmost digit.

Note: Do not convert the int to a string and check for the occurrence of 1.

hasOne(10) → true
hasOne(22) → false
hasOne(220) → false

Solution:
public boolean hasOne(int n) {
int left = n;
while (left > 0) {
int lastDigit = left % 10;
if (lastDigit == 1) {
return true;
} else {
left /= 10;
}
}
return false;
}

Task 7 (Skill Level 4)


Implement the method public int countEvens(int[] nums).

Return the number of even ints in the given array. Note: the % "mod" operator computes
the remainder, e.g. 5 % 2 is 1.

countEvens({2, 1, 2, 3, 4}) → 3
countEvens({2, 2, 0}) → 3
countEvens({1, 3, 5}) → 0

Solution:
public int countEvens(int[] nums) {
http://www.ima.rwth-aachen.de

int count = 0;
for (int n : nums) {
count += (n % 2 == 1 ? 0 : 1);
}
return count;
}

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 5

Task 8 (Skill Level 6)


Implement the method public int userCompare(String aName, int aId, String bName, int bId).

We have data for two users, A and B, each with a String name and an int id. The goal is to
order the users such as for sorting. Return -1 if A comes before B, 1 if A comes after B, and 0
if they are the same. Order first by the string names, and then by the id numbers if the
names are the same.

Note: With strings str1.compareTo(str2) returns an int value which is negative/0/positive to


indicate how str1 is ordered to str2 (the value is not limited to -1/0/1). (On the AP, there
would be two User objects, but here the code simply takes the two strings and two ints
directly. The code logic is the same.)

userCompare("bb", 1, "zz", 2) → -1
userCompare("bb", 1, "aa", 2) → 1
userCompare("bb", 1, "bb", 1) → 0

Solution:
public int userCompare(String aName, int aId, String bName, int bId) {
int comparision = aName.compareTo(bName);
if (comparision < 0) {
return -1;
} else if (comparision > 0) {
return 1;
} else {
return (int) Math.signum(aId - bId);
}
}

http://www.ima.rwth-aachen.de

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 6

Task 9 (Skill Level 10)


Implement the method public boolean groupNoAdj(int start, int[] nums, int target).

Given an array of ints, is it possible to choose a group of some of the ints, such that the
group sums to the given target with this additional constraint: If a value in the array is
chosen to be in the group, the value immediately following it in the array must not be
chosen.

Hint: No loops needed (but if you want to, you can use loops).

groupNoAdj(0, {2, 5, 10, 4}, 12) → true


groupNoAdj(0, {2, 5, 10, 4}, 14) → false
groupNoAdj(0, {2, 5, 10, 4}, 7) → false

Solution:
public boolean groupNoAdj(int start, int[] nums, int target) {
if (target == 0) {
return true;
} else if (target < 0) {
return false;
} else {
if (start >= nums.length) {
return false;
}
int value = nums[start];
return groupNoAdj(start + 2, nums, target - value) ||
groupNoAdj(start + 1, nums, target);
}
}

http://www.ima.rwth-aachen.de

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning

You might also like