forked from niallcampbell/Zalando_Coding_Challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
161 lines (121 loc) · 4.01 KB
/
Solution.java
File metadata and controls
161 lines (121 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.util.*;
/**
* Solutions to three coding questions from Zalando.
*
* @author Niall Campbell
* @version 1.0
*/
public class Solution
{
/**
* Question: count the number of words in a given String.
*/
public static int solution(String S)
{
//Count the number of sentences
//Divide sentences into seperate Strings
//Count the number of words in each String
String[] sentences = S.split("[?|!|\\.]");
int max = 0; //store the max number of words
int numSen = sentences.length; //number of sentences
//Loop through each sentence in the array and count the num of words
for(int i = 0; i < numSen; i++)
{
int numWords = 0;
String sen = sentences[i];
String[] words = sen.split(" ");
numWords = words.length;
for(int j = 0; j < numWords; j++)
{
if(words[j].equals(""))
{
numWords--;
}
}
if(numWords > max)
{
max = numWords;
}
}
return max;
}
/**
* Shuffle a given digit.
*/
public int solution2(int A)
{
//one digit from the front and one digit from the back
String val = Integer.toString(A); //convert the int to a String
String shuffle = ""; //will add the shuffled values to this string
int len = val.length(); //the amount of digits in the String
int i = 0; //the index at the start of the String
int j = len - 1; //the index from the back of the String
while(i < j)
{
shuffle += val.charAt(i);
shuffle += val.charAt(j);
i++;
j--;
}
//if the number of digits is odd
if(len % 2 != 0)
{
shuffle += val.charAt(i);
}
//convert the string back to an int
int numShuffled = Integer.parseInt(shuffle);
return numShuffled;
}
/**
* Given an array, see how many times you can make the array be in order by removing one element.
*/
public static int solution3(int[] arr)
{
//loop through the array and take out one element at a time
//put the other elements in another array
//check if this array is in order
//put the element back in and move on to the next one
int len = arr.length;
List<Integer> arr2 = new ArrayList<Integer>();
int count = 0;
for(int i = 0; i < len; i++)
{
arr2.add(arr[i]);
}
for(int i = 0; i < len; i++)
{
int temp = (int) arr2.remove(i); //returns an object so cast it to an int
if(inOrderList(arr2))
{
count++;
}
arr2.add(i, temp);
}
return count;
}
/**
* Check if a linked list is in order.
*
* For solution 3.
*/
public static boolean inOrderList(List<Integer> list)
{
int len = list.size();
for(int i = 0; i < len-1; i++)
{
int a = (int) list.get(i);
int b = (int) list.get(i + 1);
if(a > b)
{
return false;
}
}
return true;
}
public static void main()
{
System.out.print(solution("Forget your CVs! Save time. x x"));
int[] arr = {1,2,4,3,5};
System.out.println(solution3(arr));
}
}