ASE Exercise 8 (Fall 2015) : Task 1 (Skill Level 0)
ASE Exercise 8 (Fall 2015) : Task 1 (Skill Level 0)
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.
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
Solution:
public int close10(int a, int b) {
int aDiff = Math.abs(a - 10);
int bDiff = Math.abs(b - 10);
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
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"
Solution:
public String missingChar(String str, int n) {
String front = str.substring(0, n);
Given an array of ints, return true if the sequence 1, 2, 3 appears in the array somewhere.
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
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
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.
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;
}
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.
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
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
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;
}
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
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.
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
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).
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