声明
本博客所有内容来源于网络、书籍、和各类手册。
内容均为非盈利,旨为学习记录,方便查询、总结备份、开源分享。
部分转载内容均有注明出处,如有侵权请告知,马上删除.
Q1 CAFE
Since HQ is situated opposite a fruit stall in chinatown, Company is thinking of opening Company Cafe to earn more profit. The Cafe serves 3 items, with the following ingredients required:
APPLE PIE - 3x Apple
PINEAPPLE PIE - 3x Pineapple
FRUIT PARFAIT - 2x Apple + 2x Pineapple
You are an inventory manager in charge of checking if we can fulfill a customer’s orders with the ingredients we have in stock. Luckily, the inventory list is contained in a text file. Write a bash script named recipeChecker.sh which will let us know if we can fulfill the order.
Your task:
use sample fruitList.txt and implement the bash script recipeChecker.sh
Your script should take in 2 command line arguments:
Arg 1: absolute file path of the inventory list (e.g. fruitList.txt )
Arg 2: the customer’s order consisting only one of [“APPLE PIE”, “PINEAPPLE PIE” or “FRUIT PARFAIT”] in BLOCK letters
Your script should output the following to stdout:
If there are enough ingredients to create the specified dessert, output exactly ( including all punctuation) : You shall have (recipe name in block letters)!
Else, output: You shall not have (recipe name in block letters)
If an unrecognised recipe is provided (eg Pineapple Pie), output: We do not have that on the menu
Other Specifications
You should consider all fruits to be case insensitive (i.e if a list contains “Apple” and “apple” you can consider them as 2 apples)
Each line of the inventory list provided will contain only one item, and terminates with a “,”
Any item which contains “apple”, such as “apples”, “apple jam” or “rotten apple” should not be considered an “apple” ingredient.
See the following examples of how the inventory list will be formatted
Example
fruitList.txt
Pineapples,
Apple,
Pineapple,
Orange,
APPLE,
Rotten apple,
Grape,
apple,
Samples
Sample 1
Command: ./recipeChecker.sh fruitList.txt "APPLE PIE"
Output: You shall have APPLE PIE!
Explanation: Lines 2, 5, 8 contain 3 apples together
Sample 2
Command: ./recipeChecker.sh fruitList.txt "FRUIT PARFAIT"
Output: You shall not have FRUIT PARFAIT
Explanation: Lines 2, 5, 8 contains 3 apples, line 3 contains 1 pineapple. We need minimum 2 of each.
Sample 3
Command: ./recipeChecker.sh fruitList.txt "CHICKEN PIE"
Output: We do not have that on the menu
Explanation: Menu only contains PINEAPPLE PIE, APPLE PIE and FRUIT PARFAIT
Solution
recipeChecker.sh
#!/bin/bash
pwd="$1"
appleC=$(grep -c "APPLE" -i $pwd)
pinappleC=$(grep -c "pinapple" -i $pwd) #NO SPACE AFTER =
case "$2" in
"APPLE PIE")
if [ $appleC -ge 3 ] # MUST HAVE SPACE BEFORE OR AFTER [
then
echo "You shall have APPLE PIE !"
else
printf "You shall not have APPLE PIE "
fi
;;
"PINAPPLE PIE")
if [ $pinappleC -ge 3 ]
then
printf "You shall have PINAPPLE PIE !"
else
printf "You shall not have PINAPPLE PIE "
fi
;;
"FRUIT PARFAIT")
if [ $appleC -ge 2 -a $pinappleC -ge 2 ]
then
printf "You shall have FRUIT PARFAIT !"
else
printf "You shall not have FRUIT PARFAIT "
fi
;;
*) echo "We do not have that on the menu"
;;
esac
Q2 CAFE EXPANSION
As Cafe expands, they decide to increase the menu items. Unfortunately, there were too many orders that went unfulfilled. Company’s Data Scientists request for some data so the Company Cafe Logistics Team may review how to better manage the inventory based on customer demand.
You decide to write another bash script named unfulfilledOrders.sh to help the Data Scientists which will output the 3 most recent unfulfilled orders for the specified date and order, sorted by timestamp from oldest to newest.
Your task:
use sample fruitList.txt and implement the bash script unfulfilledOrders.sh
Your script should take in 3 command line arguments:
Arg 1: absolute file path of the order list (eg orderLists.txt)
Arg 2: the customer’s order
Arg 3: the order date in YYYY-MM-DD format
Other Specifications
There is an infinite list of orders that are not predefined. You can consider the orders to be case insensitive. You may also get orders which contain non-alphabetical characters.
Each line of the order list provided will contain only one order at one timestamp
If there are fewer than 3 orders which fulfill the requirements, you can just display those.
The order list provided contains orders in a random order
See the following examples of how the order list will be formatted
orderList.txt
2019-01-18 15:30:00 order="STRAWBERRY SHORTCAKE" fulfilled=FALSE
2019-01-18 16:40:00 order="STRAWBERRY SHORTCAKE" fulfilled=FALSE
2019-01-18 13:15:00 order="STRAWBERRY SHORTCAKE" fulfilled=FALSE
2019-01-18 10:15:00 order="MILLE CREPE" fulfilled=FALSE
2019-01-18 08:30:00 order="MILLE CREPE" fulfilled=TRUE
2019-01-18 13:25:00 order="STRAWBERRY SHORTCAKE" fulfilled=TRUE
2019-01-18 17:00:00 order="STRAWBERRY SHORTCAKE" fulfilled=TRUE
2019-01-17 10:30:00 order="STRAWBERRY SHORTCAKE" fulfilled=FALSE
2019-01-17 18:30:00 order="STRAWBERRY SHORTCAKE" fulfilled=FALSE
2019-01-18 12:15:00 order="STRAWBERRY SHORTCAKE" fulfilled=FALSE
2019-01-18 11:30:00 order="MILLE Crepe" fulfilled=FALSE
Samples
Sample 1
Command: ./unfulfilledOrders.sh orderList.txt “STRAWBERRY SHORTCAKE” 2019-01-18
Output:
2019-01-18 13:15:00 order=“STRAWBERRY SHORTCAKE” fulfilled=FALSE
2019-01-18 15:30:00 order=“STRAWBERRY SHORTCAKE” fulfilled=FALSE
2019-01-18 16:40:00 order=“STRAWBERRY SHORTCAKE” fulfilled=FALSE
Explanation:
Lines 1-3, 6-7 and 10 contain STRAWBERRY SHORTCAKE orders on 2019-01-18
Lines 1-3, and 10 contain unfulfilled (fulfilled=FALSE) orders
Arrange lines 1-3 and 10 in ascending order, and the last 3 timings are 13:15:00, 15:30:00 and 16:40:00
Sample 2
Command: ./unfulfilledOrders.sh orderList.txt “MILLE CREPE” 2019-01-18
Output:
2019-01-18 10:15:00 order=“MILLE CREPE” fulfilled=FALSE
2019-01-18 11:30:00 order=“MILLE Crepe” fulfilled=FALSE
Explanation:
Lines 4-5 and 11 contain MILLE CREPE orders on 2019-01-18
Lines 4 and 10 contain unfulfilled (fulfilled=FALSE) orders
Arrange lines 4 and 10 in ascending order, and the last up to 3 timings are 10:15:00 and 11:30:00
Solutions
unfulfilledOrders.sh
#!/bin/bash
rmd=$(grep $fulfilled=FALSE "$1" | grep "$2" | grep "$3" | sort | tail -n 3)
echo "$rmd"
Q3 Royal Rumble
An ordinal number is a word representing rank or sequential order. The naming convention for royal names is to follow a given name with an ordinal number using a Roman numeral to indicate the birth order of two people of the same name.
The Roman numerals from 1 to 50 are defined as follows: The numbers 1 through 10 are written I, II, III, IV, V, VI, VII, VIII, IX, and X. The Roman numerals corresponding to the numbers 20, 30, 40, and 50 are XX, XXX, XL, and L. For any other two-digit number < 50, its Roman numeral representation is constructed by concatenating the numeral(s) for its multiples of ten with the numeral(s) for its values < 10. For example, 47 is 40 + 7 = “XL” + “VII” = “XLVII”.
In this challenge, you will be given a list of royal name strings consisting of a given name followed by an ordinal number. You must sort the list first alphabetically by name, then by ordinal increasing within any given name.
For example, if you are given the royal names [George VI, William II, Elizabeth I, William I] the result of the sort is [Elizabeth I, George VI, William I, William II].
Your task:
complete the function getSortedList in RoyalRumble.
getSortedList takes in a list of royal name strings and must return the list of names sorted first by given name, then by ordinal.
Constraints:
There will be between 1 and 50 names in the list.
Each name is a single string composed of firstName and ordinal, separated by a space.
ordinal is a valid Roman numeral representing a number between 1 and 50, inclusive.
The length of firstName will be between 1 and 20.
Each firstName comprises only uppercase and lowercase ascii characters [A-Za-z].
Samples
Sample Input:
Louis IX
Louis VIII
David II
Sample Output:
David II
Louis VIII
Louis IX
Solutions
RoyalRumble.java
import java.util.List;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class RoyalRumble {
public List<String> getSortedList(List<String> names) {
String [] name=new String[names.size()] ;
name= names.toArray(name);
List<String> list=Arrays.asList(RoyalRumble.getSorting(name));
return list;
}
public static String[] getSorting (String[] name) {
for(int i = 0; i < name.length;i++){
for(int j = i+1; j< name.length;j++){
if(i != j){
String temp2 = "";
String name1 = getName(name[i]);
int order1 = getOrder(name[i]);
String name2 = getName(name[j]);
int order2 = getOrder(name[j]);
if(name1.compareTo(name2) > 0){
temp2 = name[i];
name[i] = name[j];
name[j] = temp2;
}else if(name1.compareTo(name2) == 0){
if(order1 > order2){
temp2 = name[i];
name[i] = name[j];
name[j] = temp2;
}
}
}
}
}
return name;
}
private static String getName(String obj){
return obj.split(" ")[0];
}
private static int getOrder(String obj){
return getNumeric(obj.split(" ")[1]);
}
private static int getNumeric(String roman) {
Map<Character,Integer> romanList = new HashMap<>();
romanList.put('I', 1);
romanList.put('V', 5);
romanList.put('X', 10);
romanList.put('L', 50);
romanList.put('C', 100);
romanList.put('D', 500);
romanList.put('M', 1000);
int total = 0;
int temp = 0;
char[] chars = roman.toCharArray();
for(int i = (chars.length-1); i >= 0 ;i--){
if(temp <= romanList.get(chars[i])){
total += romanList.get(chars[i]);
}else {
total -= romanList.get(chars[i]);
}
temp = romanList.get(chars[i]);
}
return total;
}
}
run.sh
#!/bin/bash
for file in ./input*.txt
do
java -cp . Main $file
printf "\n"
done
run.bat
@echo off
for %%f in (input*.txt) do (
java -cp . Main %%f
echo.
)
Q4 Defender Arcade
Company employees love playing video games, so they have Defender Arcade within the company (Work hard-Play hard). Since everyone is quite busy with work, everyone has provided the time (start time and finish time) when he or she wants to play games. If play time of two employees overlap then they start fighting and stop working. The boss got to know about this situation and asked you to help him by calculating the minimum number of Defender Arcades needed so that every employee can play during their specified time.
Note: If one employee is leaving and at the same time another employee is starting then only one Arcade is needed.
Your task:
Use the function countArcades in DefenderArcade.
countArcades takes in a list of employees’ play times and returns the number of arcade machines needed.
Constraints:
There will be between 1 and 100 time periods in the list
Each item in the list is a string composed of start time and end time, separated by a space.
Times are denoted in 24-hour format. For example 915 means 9:15AM, 2145 means 9:45PM, etc.
Output:
Single integer denoting minimum number of Defender Arcade needed.
Samples
Sample Input:
900 910
940 1200
950 1120
1100 1130
1300 1400
1350 1420
Sample Output:
3
Explanation:
Between 1100 and 1120, there are 3 employees who wants to play, so a minimum of 3 Defender Arcades are needed.
Solutions
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class DefenderArcade {
public int countArcades(List<String> times) {
int s=0;
int right[] = new int[times.size()];
int left[] = new int[times.size()];
String[]str2=null;
String[]str3=new String[2*times.size()];
Iterator ir = times.iterator();
while(ir.hasNext()){
String str=(String)ir.next();
if(str.contains(" "));
str2=str.split(" ");
str3[s]=str2[0];
str3[s+1]=str2[1];
s=s+2;
}
for (int a = 0; a < times.size(); a++) {
right[a] = Integer.parseInt(str3[a * 2]);
left[a] = Integer.parseInt(str3[a*2+1]);
}
return maxOverlapIntervalCount(right, left);
}
public static int maxOverlapIntervalCount(int[] start, int[] end){
int maxOverlap = 0;
int currentOverlap = 0;
Arrays.sort(start);
Arrays.sort(end);
int i = 0;
int j = 0;
int m=start.length,n=end.length;
while(i< m && j < n){
if(start[i] < end[j]){
currentOverlap++;
maxOverlap = Math.max(maxOverlap, currentOverlap);
i++;
}
else{
currentOverlap--;
j++;
}
}
return maxOverlap;
}
}
run.bat
@echo off
for %%f in (input*.txt) do (
java -cp . Main %%f
echo.
)
run.sh
#!/bin/bash
for file in ./input*.txt
do
java -cp . Main $file
printf "\n"
done