Skip to content

Commit a07dd91

Browse files
committed
new problem.
1 parent d94e940 commit a07dd91

File tree

5 files changed

+219
-0
lines changed

5 files changed

+219
-0
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
"Sangar",
155155
"Serializable",
156156
"Shakeel",
157+
"Shopify",
157158
"Sina",
158159
"Snapchat",
159160
"Sourcegraph",
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package Miscellaneous.src.Problems.Programming.SortCommaSeparatedProducts.Java;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class Solution {
8+
// <Name>,<Sales>,<Price>
9+
static String[] INPUT = new String[] {"Selfie Stick,98,29", "iPhone Case,90,15",
10+
"Fire TV Stick,48,49", "Wyze Cam,48,25", "Water Filter,56,49",
11+
"Blue Light Blocking Glasses,90,16", "Ice Maker,47,119", "Video Doorbell,47,199",
12+
"AA Batteries,64,12", "Disinfecting Wipes,37,12", "Baseball Cards,73,16",
13+
"Winter Gloves,32,112", "Microphone,44,22", "Pet Kennel,5,24",
14+
"Jenga Classic Game,100,7", "Ink Cartridges,88,45", "Instant Pot,98,59",
15+
"Hoze Nozzle,74,26", "Gift Card,45,25", "Keyboard,82,19"};
16+
17+
public static void main(String[] args) {
18+
List<Item> items = new ArrayList<Item>();
19+
for (String eachInput : INPUT) {
20+
String[] temps = eachInput.split(",");
21+
Item temp = new Solution.Item(temps[0], Integer.parseInt(temps[1]),
22+
Integer.parseInt(temps[2]));
23+
items.add(temp);
24+
}
25+
26+
List<Item> resultAll = getSorted(items);
27+
for (Item eachItem : resultAll) {
28+
System.out.println(eachItem.name);
29+
}
30+
}
31+
32+
public static List<Item> getSorted(List<Item> items) {
33+
Collections.sort(items);
34+
return items;
35+
}
36+
37+
static class Item implements Comparable<Item> {
38+
String name;
39+
int sales;
40+
int price;
41+
42+
public Item(String name, int sales, int price) {
43+
this.name = name;
44+
this.sales = sales;
45+
this.price = price;
46+
}
47+
48+
public int compareTo(Item otherItem) {
49+
if (this.sales == otherItem.sales) {
50+
return this.price - otherItem.price;
51+
} else {
52+
return otherItem.sales - this.sales;
53+
}
54+
}
55+
}
56+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Miscellaneous.src.Problems.Programming.SortCommaSeparatedProducts.Java;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.time.Duration;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.AfterEach;
8+
import org.junit.jupiter.api.BeforeEach;
9+
10+
public class SolutionTest {
11+
12+
Solution solution;
13+
14+
@BeforeEach
15+
public void setUp() throws Exception {
16+
solution = new Solution();
17+
}
18+
19+
@AfterEach
20+
public void tearDown() throws Exception {
21+
solution = null;
22+
}
23+
24+
@Test
25+
public void MainFunction() {
26+
assertTimeout(Duration.ofMillis(500), () -> {
27+
String[] args = new String[0];
28+
assertAll(() -> Solution.main(args));
29+
});
30+
}
31+
32+
@Test
33+
public void TrivialCase1() {
34+
String[] INPUT = new String[] {"Selfie Stick,98,29", "iPhone Case,90,15",
35+
"Fire TV Stick,48,49", "Wyze Cam,48,25", "Water Filter,56,49",
36+
"Blue Light Blocking Glasses,90,16", "Ice Maker,47,119", "Video Doorbell,47,199",
37+
"AA Batteries,64,12", "Disinfecting Wipes,37,12", "Baseball Cards,73,16",
38+
"Winter Gloves,32,112", "Microphone,44,22", "Pet Kennel,5,24",
39+
"Jenga Classic Game,100,7", "Ink Cartridges,88,45", "Instant Pot,98,59",
40+
"Hoze Nozzle,74,26", "Gift Card,45,25", "Keyboard,82,19"};
41+
assertTimeout(Duration.ofMillis(500), () -> {
42+
// expected = ;
43+
// actual = Solution.;
44+
// assertEquals(expected, actual);
45+
});
46+
}
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"type": "Coding",
3+
"name": "Simple Product Sorting",
4+
"origin": {
5+
"name": "Miscellaneous",
6+
"link": ""
7+
},
8+
"companies": [
9+
"Shopify",
10+
""
11+
],
12+
"categories": [{
13+
"name": "",
14+
"children": [{
15+
"name": "",
16+
"children": []
17+
}]
18+
},
19+
{
20+
"name": "Difficulty",
21+
"children": [{
22+
"name": "Easy",
23+
"children": []
24+
}]
25+
}
26+
],
27+
"tags": [
28+
"Arrays",
29+
"Sorting"
30+
],
31+
"buckets": [
32+
"",
33+
""
34+
]
35+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Problem Definition
2+
3+
## Description
4+
5+
Write a program that sorts a list of comma separated products.
6+
7+
Each input row looks like `"TITLE,POPULARITY,PRICE"`. Meaning `"Selfie Stick,95,29"` says we sold
8+
95 "Selfie Stick"s at 29 dollars each. All numbers are integers. The input will be provided in a
9+
hardcoded array so no file I/O is needed.
10+
11+
The list should be sorted by:
12+
13+
By most popular first. If products are equally popular, sort by cheapest price (lower is better).
14+
15+
You don't need to write your own sorting algorithm. It's recommended to use a built-in sorting
16+
library.
17+
18+
### Example
19+
20+
If the input is:
21+
22+
```plaintext
23+
"Selfie Stick,98,29",
24+
"iPhone Case,90,15",
25+
"Fire TV Stick,48,49",
26+
"Wyze Cam,48,25",
27+
"Water Filter,56,49",
28+
"Blue Light Blocking Glasses,90,16",
29+
"Ice Maker,47,119",
30+
"Video Doorbell,47,199",
31+
"AA Batteries,64,12",
32+
"Disinfecting Wipes,37,12",
33+
"Baseball Cards,73,16",
34+
"Winter Gloves,32,112",
35+
"Microphone,44,22",
36+
"Pet Kennel,5,24",
37+
"Jenga Classic Game,100,7",
38+
"Ink Cartridges,88,45",
39+
"Instant Pot,98,59",
40+
"Hoze Nozzle,74,26",
41+
"Gift Card,45,25",
42+
"Keyboard,82,19"
43+
```
44+
45+
The sorted output should be:
46+
47+
```plaintext
48+
Jenga Classic Game
49+
Selfie Stick
50+
Instant Pot
51+
iPhone Case
52+
Blue Light Blocking Glasses
53+
Ink Cartridges
54+
Keyboard
55+
Hoze Nozzle
56+
Baseball Cards
57+
AA Batteries
58+
Water Filter
59+
Wyze Cam
60+
Fire TV Stick
61+
Ice Maker
62+
Video Doorbell
63+
Gift Card
64+
Microphone
65+
Disinfecting Wipes
66+
Winter Gloves
67+
Pet Kennel
68+
```
69+
70+
## Discussion
71+
72+
### Approach
73+
74+
#### Time Complexity
75+
76+
#### Space Complexity
77+
78+
## Notes
79+
80+
## References

0 commit comments

Comments
 (0)