|
| 1 | +All of these should go in a project named 'Collections1' |
| 2 | +###0.Check if an expression of brackets is correct### |
| 3 | +Such expression is a string like `"()()())))((())("` - it has multiple brackets, your job is to check whether the expression is correct. |
| 4 | +Such expression is correct if: |
| 5 | +- it starts with a `'('` |
| 6 | +- it ends with a `')'` |
| 7 | +- the number of opening brackets equals exactly the number of closing brackets |
| 8 | +- at no point in the string the number of closing brackets is higher than the number of opening brackets. E.g. `())(()` is not a valid one, because at index 2 there is a second closing bracket with only one opening bracket. |
| 9 | + |
| 10 | +###1.Reverse a generic collection### |
| 11 | +Declare a method, expecting a Collection and reverse it in your method. Return the same collection given, do not return a new collection!. |
| 12 | +Either use: |
| 13 | +`static <T> void reverse (Collection<T> collection)` |
| 14 | +or make it a concrete type: |
| 15 | +`static void reverse(Collection<Integer> collection)` |
| 16 | + |
| 17 | +*Do not try to use* Collections.reverse. It works only for a List, and not for collections : ) |
| 18 | + |
| 19 | + |
| 20 | +###2.Implement an on/off collection### |
| 21 | +Implement a Collection, in which, if you add an element twice, the element gets removed from your collection. |
| 22 | + |
| 23 | +*Hint and edge case spoiler:* How would you handle null objects? :) |
| 24 | +*Fun fact:*: As logical idea behind a collection is an abstraction of a... well, bunch of elements, allowing null actually makes no sense. |
| 25 | +Note that the java implementations are broken on several places, allowing nulls to creep into the collections, making all kinds of havoc later on. Don't blindly follow the java implementations, do better. |
| 26 | + |
| 27 | + |
| 28 | +###3.Make a bounded queue### |
| 29 | +A bounded queue is queue with a 'limit' of maximum elements. |
| 30 | +Your `BoundedQueue` class of course should implement the `Queue` interface. |
| 31 | +Example usage of your `BoundedQueue` class: |
| 32 | +``` |
| 33 | +BoundedQueue<Integer> boundedQueue = new BoundedQueue<>(3); |
| 34 | +boundedQueue.offer(1); |
| 35 | +boundedQueue.offer(2); |
| 36 | +boundedQueue.offer(3); |
| 37 | +boundedQueue.offer(4); |
| 38 | +boundedQueue.offer(5); |
| 39 | +System.out.println(boundedQueue.toString()); //3,4,5 |
| 40 | +``` |
| 41 | +*Hint:* View the java impl and interface:) |
| 42 | + |
| 43 | +###4.Rotate the elements of a collection### |
| 44 | +Make a void *utility* method that rotates the contents of a collection. |
| 45 | + |
| 46 | +You are given a collection containing `[one, two, three, four, five, six, seven]` |
| 47 | +"Rotating" it once means the collection becoming `[seven, one, two, three, four, five, six]` |
| 48 | + |
| 49 | +```java |
| 50 | +void rotate(Collection<Integer> collection, int rotateStep ) |
| 51 | +``` |
| 52 | + |
| 53 | +`rotateStep` can be negative - meaning you are rotating to the left, and not to the right. |
| 54 | + |
| 55 | + |
| 56 | +###5.Given a list contaning some duplicate values, find the first unique element### |
| 57 | +Make a *utility* method, which returns the first unique element in a given argument of type `Collection<Integer>`. |
| 58 | +Example: |
| 59 | + |
| 60 | +```java |
| 61 | +Collection<Integer> ints = Arrays.asList(1,2,3,4,5,5,4,3,1); |
| 62 | +System.out.println(yourMethod(ints)) //2; |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | +###6.Given several sets, find the duplicating elements### |
| 67 | +Write a *utility* method, that takes several sets, and returns a set that contains the duplicating elements of all the other sets. |
| 68 | + |
| 69 | +``` |
| 70 | +A = {1,2,3,4,5} |
| 71 | +B = {4,5,6} |
| 72 | +C = {5,6,7,8} |
| 73 | +``` |
| 74 | + |
| 75 | +`yourMethd(A,B,C) => D{5}` |
| 76 | + |
| 77 | +###7.Given a list of Students, sort them by grade### |
| 78 | +Lets say you have a `List<Student>`, where a Student is a class, containing two fields: `name` and `grade`. |
| 79 | + |
| 80 | +Sort them by their grades first. Their grades actually are integers => 2,3,4,5,6. If two students happen to have the same grades, sort those two by their names. |
| 81 | + |
| 82 | +*hint* : Use Comparable or Comparator |
| 83 | + |
| 84 | +###8.Give me the median, quick!### |
| 85 | +Make an interface `Statistics`, which has these operations: |
| 86 | + |
| 87 | +``` |
| 88 | +-getMean(); |
| 89 | +-getMedian(); |
| 90 | +-getMode(); |
| 91 | +-getRange(); |
| 92 | +``` |
| 93 | + |
| 94 | +If you don't know what Mean,Median, Mode and Range are, see http://www.purplemath.com/modules/meanmode.htm |
| 95 | + |
| 96 | +Make an implementation of this interface, with an additional operation `add(int number)` (Work with integers only). |
| 97 | +I would also like to able to look through all the integers I've given you, with a `for(Integer i : isntanceOfYourClass) {... }`. |
| 98 | + |
| 99 | +So, as a client of your code, I will be adding some integers(just like in a list), and would want you to give me the mean, median, mode and range. |
| 100 | + |
| 101 | +*Bonus/Challenge* |
| 102 | +Every method from `Statistics` interface should complete in O(1) time. |
| 103 | +This is a little bit on the algorithm side, and you might need some interesting data structures : ) |
| 104 | +Come back to this when you are done with all the tasks. |
| 105 | + |
| 106 | + |
| 107 | +*Hints* |
| 108 | +Solve this one *iteratively* with TDD. |
| 109 | + |
| 110 | + |
| 111 | +###9.Implement a class ToDoList### |
| 112 | +Imagine you have a world of stuff to do. |
| 113 | +Homeworks, courseworks, exams, even preparing for HackBulgaria! |
| 114 | +Unfortunately you do not have much time - you need to eat, you need to sleep, you need to have a good time. |
| 115 | + |
| 116 | +Now, you need to sort your priorities right! Make a class `ToDoList`, which supports the following operations: |
| 117 | +`void add(Task t)` |
| 118 | +`void markFinished(Task t)` |
| 119 | +`void markCancelled(Task t)` |
| 120 | +`Task getTop()` |
| 121 | +`boolean canFinish()` |
| 122 | +`int getRemainigTime()` //calculates the time remaining after you've done all of your tasks. |
| 123 | + |
| 124 | +... where `Task` is a class which represents something you have to do. What data/methods should it have? What is common for all the tasks you need to get done? |
| 125 | +A `Task` should at the very least have a priority and a time required in order to finish. |
| 126 | +You should take an integer in your constructor - the ammount of hours available for each task. |
| 127 | + |
| 128 | +Example usage of your class: |
| 129 | + |
| 130 | +```java |
| 131 | +ToDoList todo = new ToDoList(11); //11 hours remaining! |
| 132 | +todo.addTask(new StudyForAlgebraTask(10)); //maximum priority! |
| 133 | +todo.addTask(new LearnGeometryTask()); //default priority, smaller than 10 |
| 134 | +todo.addTask(new GoOutTask(1.5f)); //default priority, smaller than 10 |
| 135 | +todo.addTask(new SleepTask()); //straight 8 hours, of course! |
| 136 | + |
| 137 | +if (todo.canFinish()){ |
| 138 | + System.out.println("Woohoo!"); |
| 139 | +} else { |
| 140 | + System.out.println("I am ...screwed :("); |
| 141 | +} |
| 142 | + |
| 143 | +System.out.println(todo.top()) //StudyForAlgebraTask |
| 144 | +System.out.println(todo.getTimeNeeded()) //sum of the time needed for every task added in todo list |
| 145 | +``` |
| 146 | + |
| 147 | +*Hints* |
| 148 | +See Comparable and Comparator classes in Java. Check out the PriorityQueue class. |
| 149 | +http://javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html |
| 150 | + |
| 151 | +Use of generic classes. |
| 152 | + |
| 153 | +###10.Make a utility method that converts a hashMap into a very human-readable string### |
| 154 | +Make a *utility* method that takes a `HashMap<? extends Object, ? extends Object>` |
| 155 | +and *returns a String*, representing the HashMap. |
| 156 | + |
| 157 | +I want you to make a utility method that does this, *do not* override `toString()` in hashMap. |
| 158 | + |
| 159 | +Example: |
| 160 | + |
| 161 | +```java |
| 162 | +String result = YourUtilsClass.yourFirstUtilMethod(map); |
| 163 | +System.out.println(result) // { key1:value1, key2:value2, key3:value3 } |
| 164 | +``` |
| 165 | + |
| 166 | +###11.Count occurences of words in text### |
| 167 | +Make a *utility* method that takes a `String text` and returns a map, which on every word in `text` maps the number of times this word has been used. |
| 168 | + |
| 169 | +See Example: |
| 170 | + |
| 171 | +```java |
| 172 | +Map<String, Integer> result = YourUtilsClass.yourSecondUtilMethod("Ninjas are all over the place! We are all going to die!"); |
| 173 | +System.out.println(YourUtilsClass.yourFirstUtilMethod(result)); |
| 174 | +``` |
| 175 | + |
| 176 | +Outputs: |
| 177 | + |
| 178 | +``` |
| 179 | +// { Ninjas:1, are:2, all:2, over:1, the:1, place!:1, We:1, going:1, to:1, die!:1 } |
| 180 | +``` |
| 181 | + |
| 182 | +Retain the ordering of the elements! |
| 183 | + |
| 184 | +<<<<<<< HEAD |
| 185 | +*Hint* |
| 186 | +LinkedHashMap |
| 187 | + |
| 188 | +###12.Cryptoanalysis fun### |
| 189 | +======= |
| 190 | +##Epic bonus!## |
| 191 | +###Cryptoanalysis fun### |
| 192 | +>>>>>>> upstream/master |
| 193 | +There is an old technique for encrypting messages - shuffling the letters. For instance, if we take the string `Godzilla`, one crypted version of it |
| 194 | +is `Mrezotti`. |
| 195 | +The cipher here is |
| 196 | +``` |
| 197 | +G=m |
| 198 | +o=r |
| 199 | +d=e |
| 200 | +i=o |
| 201 | +l=t |
| 202 | +z=z |
| 203 | +``` |
| 204 | + |
| 205 | +Your task here is to decrypth the following text: |
| 206 | +``` |
| 207 | +Ajb vnf guj luqv akjvojufq . Sk qkkj egvfs Rkhfwu Lumemu'q akhhfjvq kj vnf ohifjbojc essoxew kg Dofck Ckqve , nfsf'q Ffsjejbk Tkssfq vk qnkr vnf ohifvukuq zkujc dkz nkr ov'q bkjf Iv'q vnf qehf fxfsz quhhfs . Nk-kjf neq fxfs coxfj hf ejzvnojc kj e iwevf . Tnfz hebf hf akhifvf gks hz iweaf vnf xfsz hkhfjv I ckv ojvk vnf gosqv vfeh; vnev'q rnev vnfz veucnv hf ejb ov'ww df vnev rez ujvow I wfexf . Tnf nocnfs puewovz iwezfsq vnev akhf - ejb Dofck Ckqve oq puewovz - vnf csfevfs vnf akhifvovokj roww df ejb vnf dfvvfs ov'ww df gks vnf vfeh .Ajb gfes jkv ( ks , ewvfsjevoxfwz , bkj'v cfv vkk ftaovfb) , Tkssfq neq jk iwejq vk vuam veow ejb suj ejb wfexf noq iezanfamq dfnojb . I bkj'v nexf ejz iwejq vk hkxf erez socnv jkr . Ig rf woqvfjfb vk eww vnf suhkusq rf'b nexf 50 iwezfsq . Nkr'q jkv vnf socnv vohf vk df vnojmojc edkuv Cnfwqfe . I'h qusf vnf awud oq rksmojc vk ohiskxf vnf vfeh , duv eww vnev hevvfsq socnv jkr oq vnf Wkswb Cui .Om , vnev dov edkuv vnf 50 iwezfsq hebf hf anuamwf . Noaf kjf , Ffsjejbk .Nkr , oj ej obfew rkswb , rf rkuwb ifsneiq cfv vnfqf rksbq gskh Lumemu , rnkh rf'b womf vk dfakhf wfcfjbesz ev Cnfwqfe . Buv rf bkj'v woxf oj ej obfew rkswb , ejb ojqvfeb rf cfv vnfqf rksbq gskh Tkssfq , rnkh rf'b womf vk dfakhf e wkjc-gksckvvfj hfhksz ev Cnfwqfe . Aweq .Tnev dfojc qeob , qojaf ov'q deqoaewwz ej ohikqqodwf vk veqm vk sob kusqfwxfq kg vnf £100h gwki , ev wfeqv nf'q ckv vnf socnv evvovubf . Hussez? |
| 208 | +``` |
| 209 | + |
| 210 | +Once more, your task is not to make a program that *deciphers every possible text on the planet*, you just have to decipher *this text*. And by decipher, I mean extract the meaning of it - let's not care about missed dots, commas, word casing and such stuff. |
| 211 | + |
| 212 | +####Instructions and hints#### |
| 213 | + |
| 214 | +A usually good counter-attacking technique is to simply make a histogram of the usage of letters in the *given* text and comapre it to the histogram of letters *in the language*. |
| 215 | +For example, in the english language, the letter 'e' is the most frequently used, in a total of 13% of all letters. So there is a good chance the most commonly used letter in the given text is 'e'. |
| 216 | + |
| 217 | +You can see some frequencies and letter distribution facts here: |
| 218 | +http://en.wikipedia.org/wiki/Letter_frequency |
| 219 | +http://www.cryptograms.org/letter-frequencies.php |
| 220 | + |
| 221 | + |
| 222 | +Some hints: |
| 223 | +- Ignore one-letter words. They are likely dots or hyphens or commas, you will add them later. |
| 224 | +- Trim spaces on words after you parse them. Make sure you are not analysing empty spaces. |
| 225 | +- Watch out for letter casing! Make sure you analyze uppercase 'T' the same as downcase 't'. |
| 226 | +- Make a method `String applyCipher(Map<Character, Character> cipher)`. You will likely find a good use for it. Of course, it is not mandatory. |
| 227 | +- As this problem requires some visual examination, do *not try* to solve in TDD at first. In fact, in general, apply *TDD* only when you have something to test for. |
| 228 | +- If frequency attack turns out to be not good enough, check [the list of 5000 most used words in English](https://docs.google.com/spreadsheets/d/1LuHWIlshSqwfr3AKwvqIoXAGZrNmsPUopwfhc3DhtS4/edit#gid=0). There is a good chance some of those will be from the first words you unveil! |
| 229 | + |
| 230 | + |
0 commit comments