├── BeforeCourseProblems ├── 1-Programming0-Final-Exam │ └── README.md └── 2-Spam-and-Eggs │ └── README.md ├── Projects ├── Applications │ ├── 1-Dictionary-with-Trie │ │ └── README.md │ ├── 2-Pathfinding-with-A-Star │ │ ├── README.md │ │ └── tiles.png │ ├── 3-A-Simple-REPL │ │ └── README.md │ └── README.md ├── Games │ └── README.md └── README.md ├── README.md ├── week1 ├── 1-Java-Calculator │ └── README.md ├── 2-Reverse-Integer-Palindrome │ └── README.md ├── 3-Palindrome-Score │ └── README.md ├── 4-Max-Type-Value │ └── README.md ├── 5-Binary │ └── README.md ├── README.md ├── materials │ ├── Main.java │ └── Main1.java └── solutions │ ├── 1-Java-Calculator │ └── Calculator.java │ ├── 2-Reverse-Integer-Palindrome │ └── Reversedpalindrome.java │ ├── 3-Palindrome-Score │ └── PalindromeScore.java │ ├── 4-Max-Type-Value │ └── MaxTypeValue.java │ └── 5-Binary │ └── Binary.java ├── week2 ├── 1-Coffee │ └── README.md ├── 2-Coffee-Machine │ └── README.md ├── 3-Auto │ └── README.md ├── 4-Auto-Shop │ └── README.md ├── 5-Fraction │ └── README.md ├── 6-Fridge │ └── README.md ├── README.md └── materials │ └── source │ ├── CofeeMachine2.java │ ├── Coffee.java │ ├── Coffee2.java │ ├── CoffeeMachine.java │ ├── CoffeeMain.java │ ├── Main.java │ ├── Person.java │ ├── ReadNumbersAndSort.java │ └── Student.java ├── week3 ├── 1-Static-Methods-Over-Arrays │ ├── Arr.java │ └── README.md ├── 2-Travel-Expenses │ └── README.md ├── 4-Integer-Vector │ └── README.md ├── 5-Integer-Vector-Static │ └── README.md ├── README.md └── materials │ └── Arr.java ├── week4 ├── 1-Music-Organizer │ └── README.md ├── 2-Birthday-Ranges │ ├── BirthdayRanges.java │ ├── README.md │ └── maycamp │ │ ├── 01.ans │ │ ├── 01.in │ │ ├── 02.ans │ │ ├── 02.in │ │ ├── 03.ans │ │ ├── 03.in │ │ ├── 04.ans │ │ ├── 04.in │ │ ├── bday-ranges.zip │ │ └── description.pdf ├── 3-String-Normalizer │ ├── README.md │ └── maycamp │ │ ├── description.pdf │ │ ├── generate_tests.py │ │ ├── str001.ans │ │ ├── str001.in │ │ ├── str002.ans │ │ ├── str002.in │ │ ├── str003.ans │ │ ├── str003.in │ │ ├── str004.ans │ │ ├── str004.in │ │ ├── str005.ans │ │ ├── str005.in │ │ ├── str006.ans │ │ ├── str006.in │ │ ├── str007.ans │ │ ├── str007.in │ │ ├── str008.ans │ │ ├── str008.in │ │ ├── str009.ans │ │ ├── str009.in │ │ └── string-normalizer.zip ├── 4-Vector-of-Vectors │ └── README.md ├── 5-Books-of-Lili │ └── README.md ├── 6-Missing-Students │ ├── README.md │ └── maycamp │ │ ├── description.pdf │ │ ├── m.ans001 │ │ ├── m.ans002 │ │ ├── m.in001 │ │ ├── m.in002 │ │ └── missing-students.zip ├── 7-Max-Span │ ├── README.md │ └── maycamp │ │ ├── description.pdf │ │ ├── max-span.zip │ │ ├── max001.ans │ │ ├── max001.in │ │ ├── max002.ans │ │ ├── max002.in │ │ ├── max003.ans │ │ ├── max003.in │ │ ├── max004.ans │ │ ├── max004.in │ │ ├── max005.ans │ │ ├── max005.in │ │ ├── max006.ans │ │ └── max006.in ├── 8-Anagrams │ ├── README.md │ └── maycamp │ │ ├── anagram000.ans │ │ ├── anagram000.in │ │ ├── anagram001.ans │ │ ├── anagram001.in │ │ ├── anagram002.ans │ │ ├── anagram002.in │ │ ├── anagram003.ans │ │ ├── anagram003.in │ │ ├── anagram004.ans │ │ ├── anagram004.in │ │ ├── anagram005.ans │ │ ├── anagram005.in │ │ ├── anagram006.ans │ │ ├── anagram006.in │ │ ├── anagram007.ans │ │ ├── anagram007.in │ │ ├── anagram008.ans │ │ ├── anagram008.in │ │ ├── anagrams.zip │ │ ├── description.pdf │ │ └── gen_tests.py ├── README.md └── materials │ └── Vector.java ├── week5 ├── 1-Generic-LinkedList │ └── README.md ├── 2-More-LinkedList │ └── README.md ├── 3-Static-LinkedList │ └── README.md ├── 4-Gas-Stations │ └── README.md ├── 5-Another-LinkedList │ └── README.md ├── 6-Count-The-Words │ └── README.md └── README.md ├── week6 ├── 1-Landscape │ └── README.md ├── 2-Zombie-Apocalypse │ └── README.md ├── 3-Labirinth │ └── README.md └── README.md ├── week7 ├── 1-Brackets │ └── README.md ├── 2-Ad-Bot │ └── README.md ├── 3-String-Reverse │ └── README.md ├── 4-Stack-with-Queue │ └── README.md ├── 5-Positive-Negative │ └── README.md ├── 6-Priority-Queue │ └── README.md ├── 7-Function-Calls │ └── README.md ├── 8-Function-Calls-Extended │ ├── README.md │ └── solution │ │ ├── Context.java │ │ ├── Function.java │ │ ├── FunctionCallsExtended.java │ │ └── FunctionComposition.java └── materials │ └── Zombie │ ├── AncientSword.java │ ├── Axe.java │ ├── CasualZombie.java │ ├── Main.java │ ├── Revolver.java │ ├── Shotgun.java │ ├── ToShoot.java │ ├── ToSmash.java │ ├── Weapon.java │ └── Zombie.java └── week8 ├── 1-TreeLevel └── README.md ├── 2-Find-The-Panda └── README.md ├── 3-Lakes ├── README.md ├── example1.jpg ├── example2.jpg └── example3.jpg ├── 4-Squares-In-The-Corner ├── README.md └── app.png ├── 5-ChessBoard ├── README.md └── app.png ├── 6-Triangles ├── README.md └── example.mp4 ├── 7-Bouncing-Ball ├── Application.java ├── Canvas.java ├── Canvas2.java └── README.md └── materials ├── BST.java ├── Labirinth.java └── Node.java /BeforeCourseProblems/1-Programming0-Final-Exam/README.md: -------------------------------------------------------------------------------- 1 | # Programming 0 Final Exam 2 | 3 | In a language of your choice, solve those 4 problems - https://github.com/HackBulgaria/Programming0-1/tree/master/exam 4 | 5 | -------------------------------------------------------------------------------- /BeforeCourseProblems/2-Spam-and-Eggs/README.md: -------------------------------------------------------------------------------- 1 | # Spam and Eggs 2 | 3 | In a language of your choice, solve that problem from Programming 101 - https://github.com/HackBulgaria/Programming101-3/tree/master/week1/3-The-Final-Round#spam-and-eggs 4 | -------------------------------------------------------------------------------- /Projects/Applications/1-Dictionary-with-Trie/README.md: -------------------------------------------------------------------------------- 1 | # A Dictionary, using a Trie 2 | 3 | You are up to the task to implement a dictionary, that gives meaning to common computer science terminology. This can be a helpful glossary for the beginners who study programming. 4 | 5 | You want to keep a reference from an english world to a full explanation - what is the meaning of the world. 6 | 7 | Few examples: 8 | 9 | | Word | Meaning | 10 | | ------------- |:-------------:| 11 | | recursion | The act of a function calling itself to solve a problem | 12 | | function | Something that takes input and gives output. Can be written as `f(x) = y` | 13 | 14 | Here are the details about that problem: 15 | 16 | * **To implement the dictionary, use a Trie data structure**. [You can read more about Trie here](https://en.wikipedia.org/wiki/Trie) 17 | * Make sure your save your data to a file or a database (It is up to you). Build the trie every time you start the application. 18 | * Create a Java Swing Application ([You can use windowbuilder for help](https://eclipse.org/windowbuilder/)) that gives the ability to perform **CRUD** operations on the words and the meanings. 19 | * Have a simple searching user interface. When a word is searched, find the meaning of the word. 20 | 21 | 22 | **CRUD** means create, read, update, delete. 23 | 24 | There should be options to: 25 | 26 | * Add new word with meaning to it. 27 | * Update an existing word of meaning. 28 | * Remove a word with its meaning. 29 | * Show all words and all meanings in a table. 30 | 31 | -------------------------------------------------------------------------------- /Projects/Applications/2-Pathfinding-with-A-Star/README.md: -------------------------------------------------------------------------------- 1 | # A Pathfinding in 2D grid with A* 2 | 3 | In a Java 2D world, make a grid where you have some obstacles (can be different colors), a starting point and an ending point. 4 | 5 | You should implement such logic that when a button is clicked, the program should highlight the path from the starting point to the goal. 6 | 7 | To find the path between the two points, use the [A-Star algorithm](https://en.wikipedia.org/wiki/A*_search_algorithm) 8 | 9 | To get a good idea of what we want, look at the following pictures: 10 | 11 | ![](tiles.png) 12 | 13 | Here: 14 | 15 | * Black tiles represent walls - you cannot go there 16 | * Gray tiles (there are non right now) - represent walkable path. 17 | * Green is the start tile. 18 | * Red is the end tile. 19 | * The yellow marks the path from start to end. 20 | 21 | You can animate each step of the path or you can just show the entire path. It is up to you. 22 | 23 | **You can make diagonal moves too!** 24 | 25 | ### Help 26 | 27 | A bunch of links to get you started: 28 | 29 | * [A great description of what A-Star is and how to use it](http://buildnewgames.com/astar/) 30 | * [Manhattan distance for the heuristic function](https://en.wikipedia.org/wiki/Taxicab_geometry) 31 | * You can use the PriorityQueue from Java - http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html 32 | 33 | -------------------------------------------------------------------------------- /Projects/Applications/2-Pathfinding-with-A-Star/tiles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/Projects/Applications/2-Pathfinding-with-A-Star/tiles.png -------------------------------------------------------------------------------- /Projects/Applications/3-A-Simple-REPL/README.md: -------------------------------------------------------------------------------- 1 | # A Simple REPL 2 | 3 | In case you are missing the Python REPL, we are going to impleent one. 4 | 5 | This is continuation of [that problem](https://github.com/HackBulgaria/Programming51-1/tree/master/week7/8-Function-Calls-Extended) 6 | 7 | Make a Java Swing application that has: 8 | 9 | * An input for our code 10 | * A place to show the output (Can be the same place) 11 | 12 | Our language should understand: 13 | 14 | * Mathematical expressions with: `+-*/%()^!`, where `!` is the factorial symbol and `^` means to the power of. 15 | * Every construct in our langauge is going to be on one line. No constructs are going to span on multiple lines. 16 | * Declaration of variables. We are going to declare our variables with the keyword `var`. Our language should be statically typed: 17 | * Declaration of functions. Our functions are going to have type declaration. Functions are declared with the `def` keyword. 18 | * Our language is not going to do implicit type casting. You cannot do: `1 + "1"`. 19 | * We are going to have a few "built-in" functions. 20 | 21 | ## Types 22 | 23 | We are going to support the following types: 24 | 25 | * `integer` - you can map this to the standard `int` type in Java. 26 | * `double` - you can map this to the standard `double` type in Java. 27 | * `string` - strings are defined with **double quotes**. The operator `+` is doing string concatenation. 28 | 29 | ## Functions 30 | 31 | We are going to have the following syntax for function declarations: 32 | 33 | ``` 34 | >>> def inc (integer -> integer) x = x + 1 35 | >>> def exclamate (string -> string) word = word + "!" 36 | ``` 37 | 38 | **We call functions in the standard way: `functionName(argument)`. Nothing fancy here.** 39 | 40 | The thing in the brackets is called type declaration - this tells you how many arguments there are and what to except for their type. 41 | 42 | Our REPL should give us erros if we try to do things like that: 43 | 44 | ``` 45 | >>> var integer a = 5 46 | >>> exclamate(a) 47 | Error! exclamate expects string, but integer was given. 48 | ``` 49 | 50 | ## Built-in functions 51 | 52 | We are having the following functions, that are predefined in our language: 53 | 54 | * `def strToInt (string -> integer)` - tries to turn a string into integer 55 | * `def doubleToStr (string -> double)` - tries to turn a strin into double 56 | * `def intToStr (integer -> string)` - turns integer into string 57 | * `def doubleToStr (double -> string)` - turns double into string 58 | 59 | ## Examples 60 | 61 | We should be able to compute such expressions: 62 | 63 | ``` 64 | >>> 1 + 2! + (3 * 5) 65 | 18 66 | >>> "a" + "a" 67 | "aa" 68 | ``` 69 | 70 | We should be able to declare variables with the `var` keyword. 71 | 72 | ``` 73 | >>> var integer a = 5 74 | >>> a 75 | 5 76 | >>> a + a 77 | 10 78 | ``` 79 | 80 | A more complete example: 81 | 82 | ``` 83 | >>> var integer a = 5; 84 | >>> var integer b = 6; 85 | >>> def inc (integer -> integer) x = x + 1 86 | >>> def squareSum (integer -> integer -> integer) x y = x ^ 2 + y ^ 2 87 | >>> def exclamate (string -> string) s = s + "!" 88 | >>> "Let me give you some result " + exclamate(intToStr(squareSum(inc(a), inc(b)))) 89 | "Let me give you some result 85!" 90 | ``` 91 | 92 | ## Help 93 | 94 | The following algorithms are going to help you: 95 | 96 | * [Shunting Yard](https://en.wikipedia.org/wiki/Shunting-yard_algorith) 97 | * [Postfix Algorithm](https://en.wikipedia.org/wiki/Reverse_Polish_notation) 98 | -------------------------------------------------------------------------------- /Projects/Applications/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures, Algorithms + Visualization 2 | 3 | The main idea here is to implement an interesting data structure or algorithm and use it to create a simple application, using Swing. 4 | 5 | You can choose one of the following scenarios: 6 | 7 | 8 | * [A Dictionary using Trie](1-Dictionary-with-Trie) 9 | * [A Pathfinding with A-Star](2-Pathfinding-with-A-Star) 10 | * [A Simple REPL](3-A-Simple-REPL) 11 | 12 | -------------------------------------------------------------------------------- /Projects/Games/README.md: -------------------------------------------------------------------------------- 1 | # Classic 2D Games 2 | 3 | This project includes building a mashup of two classic 2D games into one. We want you to pick two classic or 8-bit games and create a game using ideas from both games. It will be interesting for us to see the way you think and see you use your imagination in such a project. But if you want a step by step guide you can ask us personally and/or check the things bellow: 4 | 5 | ## What classic games should we choose? 6 | 7 | Examples of classic games are: 8 | 9 | * [Tetris](https://en.wikipedia.org/wiki/Tetris) 10 | * [Tetris Attack](https://en.wikipedia.org/wiki/Tetris_Attack) 11 | * [Snake](https://en.wikipedia.org/wiki/Snake_(video_game)) 12 | * [Game of Fifteen](https://en.wikipedia.org/wiki/15_puzzle) 13 | * [Stack attack](https://play.google.com/store/apps/details?id=com.forksel.loader&hl=en) 14 | * [Mario Bros](https://en.wikipedia.org/wiki/Mario_Bros.) 15 | * [Pinball](https://en.wikipedia.org/wiki/Full_Tilt!_Pinball#3D_Pinball_for_Windows_.E2.80.93_Space_Cadet) 16 | 17 | ## Note 18 | 19 | There are many more games that you can use. You have to pick two of these and merge them into one game. 20 | 21 | If you need in-depth explanation on any of the games - just ask! I would be more than happy to explain and show it to you! 22 | 23 | ## Rules 24 | 25 | The game you chose should have a 2D visualisation and should be playable with one player. 26 | 27 | ## Advices 28 | 29 | Try to work as much Object Oriented as possible. Working with objects makes game development much easier! 30 | 31 | ## Help 32 | 33 | It is a good idea to first create a simple classic 2D game. 34 | 35 | You can follow this tutorial - 36 | 37 | When you feel comfortable with what is happening - then, go and start combining things. 38 | 39 | If you are looking for game art, check this - 40 | -------------------------------------------------------------------------------- /Projects/README.md: -------------------------------------------------------------------------------- 1 | # End Projects for the Course 2 | 3 | * The projects should be done in teams of 2. 4 | * Pick one project 5 | * Each project is going to have a visual part - Java 2D game or a Swing application. 6 | 7 | ## Materials for Git & Working in teams 8 | 9 | You are going to use Git. Here are some essential Git & GitHub materials: 10 | 11 | * 12 | * 13 | * 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Programming51-1 2 | 3 | ## Partners 4 | 5 | The course is happening thanks to: 6 | 7 | [![Nemetschek Bulgaria](https://hackbulgaria.com/media/partner_logoes/logo_transparent_w200.png)](http://www.nemetschek.bg/) 8 | 9 | 10 | ## Videos 11 | 12 | | Тема | Слайдове | Допълнителни материали за четене | 13 | |------|----------|----------------------------------| 14 | | [Уводно видео за поредицата](https://www.youtube.com/watch?v=hDPp6gV_Is0) | [Цък.](http://slides.com/hackbulgaria/programming-51-videos-for-data-structures) | В презентацията. | 15 | | [Сложност на алгоритми](https://www.youtube.com/watch?v=3V14F9suOLo) | [Цък.](http://slides.com/hackbulgaria/programming51-algo-complexity) | В презентацията. | 16 | | [Интуиция за структури от данни](https://www.youtube.com/watch?v=GFiAsl7AVRM) | [Цък.](http://slides.com/hackbulgaria/programming51-intuition-for-data-structures) | [Java: what's the big-O time of declaring an array of size n?](http://stackoverflow.com/questions/5640850/java-whats-the-big-o-time-of-declaring-an-array-of-size-n) | 17 | 18 | 19 | ## Course weeks 20 | 21 | | Week | Date | Tasks | Presentation | 22 | |---------|-------- |---------|-------------| 23 | | 1 | Monday - 08.06|[Tasks for week1](week1/) | [Presentation for week1](http://slides.com/hackbulgaria/deck-17/) | 24 | | 2 | Monday - 15.06 | [Tasks for week2](week2/) | [Presentation for week2 about OOP](http://slides.com/hackbulgaria/oop) | 25 | | 2 | Wednesday - 17.06 | [Tasks for week2](week2/) | [Presentation for week2 about OOP](http://slides.com/hackbulgaria/oop-19) | 26 | | 2 | Saturday - 20.06 | [Tasks for week2](week2/) | [OOP Recap](http://slides.com/hackbulgaria/deck-20) | 27 | | 3 | Monday - 22.06 | [Tasks for week3](week3/) | [OOP and Array Recap](http://slides.com/hackbulgaria/deck-21) | 28 | | 3 | Wednesday - 24.06 | [Tasks for week3](week3/) | [Vectors](http://slides.com/hackbulgaria/deck-21-22#/) | 29 | | 4 | Monday - 29.06 | [Tasks for week4](week4/) | [Generic Types](http://slides.com/hackbulgaria/generic-types) | 30 | | 5 | Monday - 06.07 | [Tasks for week5](week5/) | [Linked List](http://slides.com/hackbulgaria/deck-24) | 31 | | 6 | Monday - 13.07 | [Tasks for week6](week6/) | | 32 | | 7 | Monday - 20.07 | [Tasks for week7](week7/) | [Interfaces and Polymorphysm](http://slides.com/hackbulgaria/deck-26) | 33 | | 8 | Monday - 27.07 | [Tasks for week8](week8/) | [Balanced Search Tree](http://slides.com/hackbulgaria/avl) | 34 | 35 | This course comes after Programming0 and before Programming 101. Hence - 51! This will be about OOP & Data Structures. 36 | 37 | The language of choice is going to be Java. The reason here is simple - Python's OOP contains too much magic for a beginner to handle. So we are going to use the clasiccal approach with Java! 38 | 39 | In order to understand that course, please check: 40 | 41 | * [Programming 0 Course](http://github.com/HackBulgaria/Programming0-1) 42 | * [This article - After Programming 0](http://blog.hackbulgaria.com/after-programming-0/) 43 | 44 | 45 | ## Program 46 | 47 | **Here is a high-level overview of the program** - what we are going to cover and use in this course: 48 | 49 | 1. Get familiar with Java - we are going to compare Java with the things we know in Python so far. We are going to talk about types, arrays, IDE, Java Virtual Machine, `public static void main`, etc. 50 | 2. Solve some problems with Java - we are going to revisit some of the problems from Programming 0 and resolve them with Java. 51 | 3. Introducing OOP. Here, the fun begins. We are going to talk about why does OOP exists, what is the philosophy there and how can we achieve things in Java, using classes, encapsulating data and learning about interfaces and polymorphism. 52 | 4. Introducing Abstract Data Structures & starting to think with interfaces. We are going to start with **linear data structures** 53 | 5. We are going to implement our first data structure - a `Vector` - a self-resizable array. After this, we are going to implement `LinkedList` - again, a self-resizable list. 54 | 6. After this, we are going to do `Queue` and `Stack`, using our previous data structures. We are going to solve a lot of problems involving queues and stacks ;) 55 | 7. To merge things, we are going to implement a `Deque` - something that behaves like a `Queue` and a `Stack`! 56 | 8. Making our way towards **non-linear data structures**, we are going to implement a mathematical `Set` data structure, the naive way. 57 | 9. After this, we are going to jump right into `Trees`, making different kind of search trees - `BinarySearchTree` and `Trie`. 58 | 10. After the trees, are are going to take a look into `Graphs` - how to implement them and what are the basics algorithms that we can run there. 59 | 11. And to finish the course, we are going to revisit some of the data structures, and implement them better, with the latest knowledge we have. 60 | 61 | 62 | Also, we are going to cover some of the concepts from the Java language: 63 | 64 | * Exceptions 65 | * Generics 66 | * Input / Output 67 | 68 | -------------------------------------------------------------------------------- /week1/1-Java-Calculator/README.md: -------------------------------------------------------------------------------- 1 | # Java Calculator 2 | 3 | In a `com.hackbulgaria.programming51.week1` package create a `Calculator` class, where you: 4 | 5 | * read one integer `a` from the standard input 6 | * read one character `oper` - the operation of the calculator - from the standard input 7 | * read one more integer `b` 8 | * output the result from `a oper b` to the console. 9 | 10 | We are going to cover the following operations: 11 | 12 | * Standard arithmetics - `+`, `-`, `*`, `/` 13 | * Exponentiation - `^`. If the input is `2 ^ 8` you should output 256 14 | * A special `?` operator, which does the following: `a ? b = a! + b!` where `a!` is the factorial of `a` 15 | 16 | Here is the code to read from the standard input: 17 | 18 | ```java 19 | package com.hackbulgaria.programming51.week1; 20 | 21 | import java.util.Scanner; 22 | 23 | public class Calculator { 24 | 25 | public static void main(String[] args) { 26 | Scanner scanner = new Scanner(System.in); 27 | int a = scanner.nextInt(); 28 | char oper = scanner.next().charAt(0); 29 | int b = scanner.nextInt(); 30 | 31 | System.out.println(a); 32 | System.out.println(oper); 33 | System.out.println(b); 34 | } 35 | 36 | } 37 | ``` 38 | 39 | 40 | ## Examples 41 | 42 | Input: 43 | 44 | ``` 45 | 1 + 2 46 | ``` 47 | 48 | Output: 49 | 50 | ``` 51 | 3 52 | ``` 53 | 54 | --- 55 | 56 | Input 57 | 58 | ``` 59 | 2 ^ 8 60 | ``` 61 | 62 | Output 63 | 64 | ``` 65 | 256 66 | ``` 67 | 68 | --- 69 | 70 | Input 71 | 72 | ``` 73 | 5 ? 2 74 | ``` 75 | 76 | Output 77 | 78 | ``` 79 | 122 80 | ``` 81 | -------------------------------------------------------------------------------- /week1/2-Reverse-Integer-Palindrome/README.md: -------------------------------------------------------------------------------- 1 | # Is n + nReveresed a palindrome 2 | 3 | In a `com.hackbulgaria.51.week1` package, create a `IntegerPalindrome` class, where you: 4 | 5 | * read one integer `n` from the standard input 6 | * Output `"YES"` **if the sum of `n` and the reversed number `n`** is palindrome. 7 | * Output `"NO"` otherwise. 8 | 9 | An integer `n` is palindrome if you reverse it and get the same number `n`. 10 | 11 | For example, `121` is a palindrome. 12 | 13 | ## Examples 14 | 15 | Input: 16 | 17 | ``` 18 | 123 19 | ``` 20 | 21 | Output: 22 | 23 | We have to calculate `123 + 321 = 444` and check if `444` is a palindrome. 24 | 25 | ``` 26 | YES 27 | ``` 28 | 29 | --- 30 | 31 | Input: 32 | 33 | ``` 34 | 5 35 | ``` 36 | 37 | Output: 38 | 39 | We have to calculate `5 + 5 = 10` and check if `10` is a palindrome. 40 | 41 | ``` 42 | NO 43 | ``` 44 | 45 | --- 46 | 47 | Input: 48 | 49 | ``` 50 | 48 51 | ``` 52 | 53 | Output: 54 | 55 | We have to calculate `48 + 84 = 132` and check if `132` is a palindrome 56 | 57 | ``` 58 | NO 59 | ``` 60 | 61 | --- 62 | 63 | Input: 64 | 65 | ``` 66 | 132 67 | ``` 68 | 69 | Output: 70 | 71 | We have to calculate `132 + 321 = 363` and check if `363` is a palindrome. 72 | 73 | ``` 74 | YES 75 | ``` 76 | -------------------------------------------------------------------------------- /week1/3-Palindrome-Score/README.md: -------------------------------------------------------------------------------- 1 | # Palindrome Score 2 | 3 | We denote the "Palindrome score" of an integer by the following function: 4 | 5 | * `P(n) = 1`, if `n` is palindrome 6 | * `P(n) = 1 + P(s)` where `s` is the sum of `n` and the `reverse of n` 7 | 8 | In a `com.hackbulgaria.51.week1` package, in a `PalindromeScore` class, implement a program that: 9 | 10 | * Reads one integer `n` from the standard input 11 | * Output the palindrome score for `n` 12 | 13 | ## Examples 14 | 15 | Lets see two examples: 16 | 17 | * `P(121) = 1`, because `121` is a palindrome. 18 | * `P(48) = 3`, because: 19 | 20 | 1. `P(48) = 1 + P(48 + 84) = 132` 21 | 1. `P(132) = 1 + 1 + P(132 + 321 = 363)` 22 | 1. `P(363) = 1`. 23 | 1. When we return from the recursion, we get 3. 24 | 25 | ## Help 26 | 27 | This is a recursive problem by nature. But try to solve it with a `while(true) {}` cycle. Break when you think it is time to break. 28 | -------------------------------------------------------------------------------- /week1/4-Max-Type-Value/README.md: -------------------------------------------------------------------------------- 1 | # Max-Type-Value 2 | 3 | In a `com.hackbulgaria.programming51.week1` package create a `MaxTypeValue` class, where you: 4 | 5 | ## Max Int Value 6 | 7 | Try to find the maximum value of `int` with a `while`/`for` cycle: 8 | 9 | * What will happen if you increase an `int` variable a lot of times? 10 | * Can you increase a positive `int` value so many times, that it gets negative? 11 | * Try to do that in a Java program. 12 | 13 | Check if your answers is the same as: 14 | 15 | ```java 16 | System.out.println(Integer.MAX_VALUE); 17 | ``` 18 | 19 | ## Max Long Value 20 | 21 | Try to find the maximum value of `long`, `float` and `double` variables. 22 | 23 | * A `long` variable can contain numbers without a floating point. Guess the difference between `int` and `long` 24 | * A `float` variable is a number that has a floating point. Like 2.6 or 45.832 25 | * A `double` variable is also a number with a floating point. Try to guess the difference between `float` and `double` 26 | -------------------------------------------------------------------------------- /week1/5-Binary/README.md: -------------------------------------------------------------------------------- 1 | # Binary 2 | 3 | In a `com.hackbulgaria.programming51.week1` package create a `Binary` class, where you create the following methods and test them in `main`: 4 | 5 | ## Int to Binary 6 | 7 | Create a method `public static String toBinary(int n){}` that converts an integer value to its binary representation. 8 | 9 | ### Examples 10 | 11 | Input: 12 | 13 | ``` 14 | 10 15 | ``` 16 | 17 | Output: 18 | 19 | ``` 20 | 1010 21 | ``` 22 | 23 | ## Binary to String 24 | 25 | Create a method `public static int toNumber(String binary){}` that converts a binary number to its integer representation. 26 | 27 | ### Examples 28 | 29 | Input: 30 | 31 | ``` 32 | 1011 33 | ``` 34 | 35 | Output: 36 | 37 | ``` 38 | 11 39 | ``` 40 | 41 | ## Count bits 42 | 43 | Create a method `public static int countBits(int n){}` that counts the bits in a decimal number. That is, the length of the binary number, without the leading zeroes. 44 | 45 | ### Examples 46 | 47 | Input: 48 | 49 | ``` 50 | 18 51 | ``` 52 | 53 | Output: 54 | 55 | ``` 56 | 5 57 | ``` 58 | 59 | ## Is Power Of Two 60 | 61 | Create a method `public static boolean isPowerOfTwo(String binary){}` that returns if the String `binary` is a power of two. 62 | 63 | ### Examples 64 | 65 | Input: 66 | 67 | ``` 68 | 10100 69 | ``` 70 | 71 | Output: 72 | 73 | ``` 74 | false 75 | ``` 76 | 77 | Input: 78 | 79 | ``` 80 | 1000 81 | ``` 82 | 83 | Output: 84 | 85 | ``` 86 | true 87 | ``` 88 | -------------------------------------------------------------------------------- /week1/README.md: -------------------------------------------------------------------------------- 1 | # Week 1 2 | 3 | ## Development environment setup 4 | 5 | Things you need to start this course: 6 | 7 | * [Install Java and JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html) 8 | * [Install Eclipse](https://eclipse.org/) 9 | * [How to install Java on Ubuntu / Linux](http://stackoverflow.com/questions/14788345/how-to-install-jdk-on-ubuntu-linux) 10 | * **Bookmark this repository** 11 | 12 | ## Java Types & Binary Numbers 13 | 14 | * [Java Primitive Types](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) 15 | * [Binary Numbers from Wikipedia](http://en.wikipedia.org/wiki/Binary_number) 16 | * [Binary Representations](http://www.swarthmore.edu/NatSci/echeeve1/Ref/BinaryMath/NumSys.html) 17 | -------------------------------------------------------------------------------- /week1/materials/Main.java: -------------------------------------------------------------------------------- 1 | package datastructures; 2 | 3 | public class Main { 4 | 5 | /** 6 | * @param args 7 | */ 8 | public static void main(String[] args) { 9 | /* 10 | * int a = 5; int b = 13; System.out.println("a + b = " + (a + b)); 11 | * 12 | * for (int i = 0; i < 10; i++) { System.out.println("i = " + i); } 13 | * 14 | * if (a == b) { System.out.println("a is smaller!"); } else { 15 | * System.out.println("b is smaller!"); } 16 | * 17 | * int i = 0; while (i < 10) { System.out.println("i = " + i); i++; } 18 | * 19 | * i = 0; do { System.out.println("i = " + i); if(i >= 10){ break; } i++; } 20 | * while (true); 21 | */ 22 | 23 | int[] a = { 1, 2, 3, 4, 5 }; 24 | System.out.println(a[0]); 25 | a[1] = 15; 26 | System.out.println(a[1]); 27 | 28 | int[] b = new int[10]; 29 | 30 | char c = 'h'; 31 | 32 | char [] str = new char[20]; 33 | 34 | for (int i = 10; i < 20; i++) { 35 | b[i - 10] = i; 36 | } 37 | 38 | for (int i = 0; i < 10; i++) { 39 | System.out.println(b[i]); 40 | } 41 | 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /week1/materials/Main1.java: -------------------------------------------------------------------------------- 1 | package week1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | /** 8 | * @param args 9 | */ 10 | public static void main(String[] args) { 11 | // TODO Auto-generated method stub 12 | 13 | /* 14 | * String name = "Kamen"; String sirname = "Kotsev"; 15 | * 16 | * int a = 612341234; String fullname = name + " " + sirname + a; 17 | * 18 | * System.out.println(fullname); 19 | */ 20 | /* 21 | * int [] a = new int[5]; 22 | * 23 | * a[5] = 3; 24 | */ 25 | 26 | double a = 5; 27 | double b = 2; 28 | 29 | Scanner input = new Scanner(System.in); 30 | a = input.nextDouble(); 31 | b = input.nextDouble(); 32 | 33 | System.out.println( devider(a,b) ); 34 | 35 | } 36 | 37 | public static double devider(double a, double b){ 38 | double c = a/b; 39 | return c; 40 | } 41 | 42 | public static String reverse(String name) { 43 | 44 | String tempName = ""; 45 | 46 | for (int i = 0; i < name.length(); i++) { 47 | tempName = name.charAt(i) + tempName; 48 | } 49 | 50 | return tempName; 51 | } 52 | 53 | public static int sum(int x, int y) { 54 | return x + y; 55 | } 56 | 57 | public static boolean isEven(int num) { 58 | return num % 2 == 0; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /week1/solutions/1-Java-Calculator/Calculator.java: -------------------------------------------------------------------------------- 1 | package week1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Calculator { 6 | 7 | public static void main(String[] args) { 8 | Scanner scanner = new Scanner(System.in); 9 | int a = scanner.nextInt(); 10 | char oper = scanner.next().charAt(0); 11 | int b = scanner.nextInt(); 12 | 13 | if (oper == '+') { 14 | System.out.println(a + " + " + b + " = " + (a + b)); 15 | } 16 | 17 | if (oper == '-') { 18 | System.out.println(a + " - " + b + " = " + (a - b)); 19 | } 20 | 21 | if (oper == '*') { 22 | System.out.println(a + " * " + b + " = " + (a * b)); 23 | } 24 | 25 | if (oper == '/') { 26 | System.out.println(a + " / " + b + " = " + (a / b)); 27 | } 28 | 29 | if (oper == '^') { 30 | int p = 1; 31 | for (int i = 0; i < b; i++) { 32 | p *= a; 33 | } 34 | System.out.println(a + " ^ " + b + " = " + p); 35 | } 36 | 37 | if (oper == '?') { 38 | int factA = 1; 39 | for(int i=1;i<=a;i++){ 40 | factA *= i; 41 | } 42 | 43 | int factB = 1; 44 | for(int i=1;i<=b;i++){ 45 | factB *= i; 46 | } 47 | System.out.println(a + " ? " + b + " = " + (factA + factB)); 48 | } 49 | 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /week1/solutions/2-Reverse-Integer-Palindrome/Reversedpalindrome.java: -------------------------------------------------------------------------------- 1 | package week1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ReversedPalindrome { 6 | 7 | /** 8 | * @param args 9 | */ 10 | public static void main(String[] args) { 11 | 12 | Scanner input = new Scanner(System.in); 13 | int n = input.nextInt(); 14 | 15 | int nRev = 0; 16 | int nTemp = n; 17 | // reverse n 18 | while (nTemp > 0) { 19 | nRev = (nRev * 10) + nTemp % 10; 20 | nTemp = nTemp / 10; 21 | } 22 | 23 | // check if n + nRev is a palindrome 24 | 25 | int nSum = n + nRev; 26 | int nSumRev = 0; 27 | while (nSum > 0) { 28 | nSumRev = (nSumRev * 10) + nSum % 10; 29 | nSum = nSum / 10; 30 | } 31 | 32 | // check if (n + nRev) == Reversed(n + nRev) to see if the sum is a 33 | // palindrome 34 | if (nSumRev == n + nRev) { 35 | System.out.println("Yes"); 36 | } else { 37 | System.out.println("No"); 38 | } 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /week1/solutions/3-Palindrome-Score/PalindromeScore.java: -------------------------------------------------------------------------------- 1 | package week1; 2 | 3 | public class PalindromeScore { 4 | 5 | /** 6 | * @param args 7 | */ 8 | public static void main(String[] args) { 9 | 10 | int a = 48; 11 | int counter = 1; 12 | while (true) { // reverse 13 | int num = a; 14 | int rev = 0; 15 | while (num > 0) { 16 | rev *= 10; 17 | rev += num % 10; 18 | num /= 10; 19 | } 20 | if (a == rev) { 21 | break; 22 | } 23 | a = a + rev; 24 | counter++; 25 | } 26 | 27 | System.out.println(counter); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /week1/solutions/4-Max-Type-Value/MaxTypeValue.java: -------------------------------------------------------------------------------- 1 | package week1; 2 | 3 | public class MaxTypeValue { 4 | 5 | /** 6 | * @param args 7 | */ 8 | public static void main(String[] args) { 9 | 10 | int a = 0; 11 | while (a >= 0) { 12 | a += 50000; 13 | System.out.println(a); 14 | } 15 | 16 | long b = 0; 17 | while(b>=0){ 18 | b += 1000000000000000l; // This lower-case L means that the number I typed is in Long format 19 | System.out.println(b); 20 | } 21 | 22 | // This is an Endless Loop, because float does not overflow due to the way it is presented in the memory 23 | float c = 0; 24 | while( c>= 0){ 25 | c += 100000000000000000000000000000000000000f; 26 | System.out.println(c); 27 | } 28 | 29 | // This is also an Endless Loop because double is presented differently in the computer memory 30 | double d = 0; 31 | while( d>= 0){ 32 | // Yes, double can hold such a big number 33 | d += 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d; 34 | System.out.println(d); 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /week1/solutions/5-Binary/Binary.java: -------------------------------------------------------------------------------- 1 | package week1; 2 | 3 | public class Binary { 4 | 5 | public static String toBinary(int n) { 6 | String binary = ""; 7 | while (n > 0) { 8 | int lastBinDigit = n % 2; 9 | binary = lastBinDigit + binary; 10 | 11 | n = n / 2; 12 | } 13 | return binary; 14 | } 15 | 16 | public static int toNumber(String binary) { 17 | int n=0; 18 | int len = binary.length() -1; 19 | for (int i = len; i >= 0; i--) { 20 | if (binary.charAt(i) == '1') { 21 | n += pow(2,(len-i)); 22 | } 23 | } 24 | 25 | return n; 26 | } 27 | 28 | public static int pow(int num, int power){ 29 | int sum = 1; 30 | for(int i=0;i0){ 39 | n = n / 2; 40 | counter++; 41 | } 42 | return counter; 43 | } 44 | 45 | public static boolean isPowerOfTwo(String binary){ 46 | int counter = 0; 47 | for(int i=0;i= machineCapacity) { 27 | return false; 28 | } 29 | 30 | if(hasKind(kind)) { 31 | return false; 32 | } 33 | 34 | kinds[currentKindsIndex] = kind; 35 | currentKindsIndex += 1; 36 | 37 | return true; 38 | } 39 | 40 | public double buyCoffee(Coffee coffee, double moneyTaken) { 41 | if(!hasKind(coffee)) { 42 | return -1; 43 | } 44 | 45 | double change = moneyTaken - coffee.price; 46 | 47 | if(change < 0) { 48 | return -1; 49 | } 50 | 51 | balance += coffee.price; 52 | 53 | return change; 54 | } 55 | 56 | public double getBalance() { 57 | return balance; 58 | } 59 | } -------------------------------------------------------------------------------- /week2/materials/source/CoffeeMain.java: -------------------------------------------------------------------------------- 1 | package week2; 2 | 3 | public class CoffeeMain { 4 | 5 | /** 6 | * 7 | * 8 | * @param args 9 | */ 10 | public static void main(String[] args) { 11 | CoffeeMachine hack = new CoffeeMachine(); 12 | Coffee java = new Coffee("Java0", 10); 13 | 14 | for(int i = 0; i < 100; i += 1) { 15 | Coffee currentJava = new Coffee("Java" + i, 10); 16 | hack.addCoffee(currentJava); 17 | } 18 | 19 | System.out.println(java); 20 | 21 | hack.buyCoffee(java, 10); 22 | hack.buyCoffee(java, 10); 23 | hack.buyCoffee(java, 10); 24 | hack.buyCoffee(java, 10); 25 | hack.buyCoffee(java, 10); 26 | hack.buyCoffee(java, 10); 27 | hack.buyCoffee(java, 10); 28 | hack.buyCoffee(java, 10); 29 | hack.buyCoffee(java, 10); 30 | hack.buyCoffee(java, 10); 31 | hack.buyCoffee(java, 10); 32 | 33 | System.out.println(hack.getBalance()); 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /week2/materials/source/Main.java: -------------------------------------------------------------------------------- 1 | package week2; 2 | 3 | import week2.CoffeeMachine; 4 | import week2.Coffee; 5 | 6 | public class Main { 7 | 8 | /** 9 | * @param args 10 | */ 11 | public static void main(String[] args) { 12 | Student kamen = new Student("Kamen","Prog51","male",51); 13 | //System.out.println(kamen); 14 | /* 15 | kamen.addGrade(3,0); 16 | kamen.addGrade(3,1); 17 | kamen.addGrade(3,2); 18 | kamen.addGrade(2,3); 19 | kamen.addGrade(3,4); 20 | 21 | System.out.println(kamen.isFailing()); 22 | kamen.shallIPass(); 23 | System.out.println(3.0 / 2.0);*/ 24 | 25 | Student [] p51 = new Student[5]; 26 | for(int i=0;i<5;i++){ 27 | p51[i] = new Student("Kamen","Prog51","male",51); 28 | } 29 | 30 | p51[0].name = "Ivan"; 31 | System.out.println(p51[0]); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /week2/materials/source/Person.java: -------------------------------------------------------------------------------- 1 | package com.hackbulgaria.programming51.week2; 2 | 3 | public class Person { 4 | private String name; 5 | private int age; 6 | 7 | public static void haha() { 8 | System.out.println("Haha!"); 9 | } 10 | 11 | public String getName() { 12 | return this.name; 13 | } 14 | 15 | public Person(String name, int age) { 16 | this.name = name; 17 | this.age = age; 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /week2/materials/source/ReadNumbersAndSort.java: -------------------------------------------------------------------------------- 1 | package com.hackbulgaria.programming51.week2; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | 7 | public class ReadNumbersAndSort { 8 | public static void main(String args[]) { 9 | Scanner scanner = new Scanner(System.in); 10 | int n = scanner.nextInt(); 11 | 12 | int[] numbers = new int[n]; 13 | 14 | for(int i = 0; i < n; i++) { 15 | int next = scanner.nextInt(); 16 | numbers[i] = next; 17 | } 18 | 19 | Arrays.sort(numbers); 20 | 21 | for(int number: numbers) { 22 | System.out.println(number); 23 | } 24 | 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /week2/materials/source/Student.java: -------------------------------------------------------------------------------- 1 | package week2; 2 | 3 | public class Student { 4 | public String name; 5 | public String course; 6 | public String gender; 7 | public int id; 8 | public int[] grades = new int[5]; 9 | 10 | public Student() { 11 | 12 | } 13 | 14 | public Student(String name, String course, String gender, int id) { 15 | this.name = name; 16 | this.course = course; 17 | this.gender = gender; 18 | this.id = id; 19 | } 20 | 21 | public void addGrade(int grade, int index){ 22 | grades[index] = grade; 23 | } 24 | 25 | public String toString() { 26 | return name + "\n" + course + "\n" + gender + "\n" + id + "\n" + printGrades(); 27 | } 28 | 29 | public String printGrades(){ 30 | String str = ""; 31 | for(int i=0; i<5; i++){ 32 | str = str + i +":" + grades[i] + "\n"; 33 | } 34 | return str; 35 | } 36 | 37 | public boolean isFailing(){ 38 | return averageGrade() < 3; 39 | } 40 | 41 | public void shallIPass(){ 42 | if(isFailing()){ 43 | System.out.println("You shall not pass!"); 44 | }else{ 45 | System.out.println("You pass this course"); 46 | } 47 | } 48 | 49 | public double averageGrade(){ 50 | double sum = 0; 51 | for(int i=0;i<5;i++){ 52 | sum = sum + grades[i]; 53 | } 54 | return sum/5; 55 | } 56 | 57 | 58 | } 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /week3/1-Static-Methods-Over-Arrays/Arr.java: -------------------------------------------------------------------------------- 1 | package com.hackbulgaria.programming51.week3; 2 | 3 | public class Arr { 4 | public static String toString(int[] a) { 5 | return ""; 6 | } 7 | 8 | public static void sort(int[] a) { 9 | 10 | } 11 | 12 | public static int[] reverse(int[] a) { 13 | return new int[a.length]; 14 | } 15 | 16 | public static String join(int[] a, String glue) { 17 | return ""; 18 | } 19 | 20 | public static int sum(int[] a) { 21 | return 0; 22 | } 23 | 24 | public static int[] range(int a, int b) { 25 | return new int[Math.abs(b - a)]; 26 | } 27 | 28 | public static int[] filterOdd(int[] a) { 29 | return new int[a.length]; 30 | } 31 | 32 | public static void main(String[] args) { 33 | int[] a = {10, 20, -50, 80, 70, 66, -365}; 34 | 35 | System.out.println("Print the array to string:"); 36 | System.out.println(Arr.toString(a)); 37 | 38 | System.out.println("Sort the array a itself and print it sorted:"); 39 | Arr.sort(a); 40 | System.out.println(Arr.toString(a)); 41 | 42 | 43 | System.out.println("Print the reverse of the sorted array"); 44 | System.out.println(Arr.reverse(a)); 45 | 46 | System.out.println("Output each element in a with -> between them"); 47 | System.out.println(Arr.join(a, "->")); 48 | 49 | System.out.println("Output the sum"); 50 | System.out.println(Arr.sum(a)); 51 | 52 | System.out.println("Output array with elements from 1 to 10"); 53 | System.out.println(Arr.toString(Arr.range(1, 10))); 54 | 55 | System.out.println("Print only the odd numbers"); 56 | System.out.println(Arr.filterOdd(a)); 57 | } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /week3/1-Static-Methods-Over-Arrays/README.md: -------------------------------------------------------------------------------- 1 | # 10-Static-Array 2 | 3 | In a `com.hackbulgaria.programming51.week3` package do the following: 4 | 5 | ## Define a `class Arr` where you 6 | 7 | ### Define the following 8 | 9 | * `public static String toString(int[] a)` method that recieves an integer array and returns a string of the array, separated by "," 10 | * `public static void sort(int[] a)` method that recieves an array of int and sorts it 11 | * `public static int[] reverse(int[] a)` method that recieves an integer array and returns the reversed array 12 | * `public static String join(int[] a, String glue)` method that recieves an array of int and a string and returns a String of the array elements with 'glue' between them 13 | * `public static int sum(int[] a)` method that returns the sum of all the elements in 'a' 14 | * `public static int[] range(int a, int b)` that returns an array of int with elements in the range between a and b 15 | * `public static int[] filterOdd(int[] a)` method that returns an array with only the Odd numbers from 'a' 16 | 17 | ## Examples 18 | 19 | You can open [Arr.java](Arr.java) and take the boilerplate code from it. 20 | 21 | 22 | ### Example for `toString(int [] a)` 23 | 24 | ```java 25 | int [] a = {1,2,3,4,5}; 26 | System.out.println(Arr.toString(a)); 27 | ``` 28 | 29 | Should print: 30 | 31 | ``` 32 | 1, 2, 3, 4, 5 33 | ``` 34 | 35 | ### Example for `sort(int [] a)` 36 | 37 | ```java 38 | int [] a = {3,1,-40,200,5}; 39 | Arr.sort(a); 40 | System.out.println(Arr.toString(a)); 41 | ``` 42 | 43 | Should print: 44 | 45 | ``` 46 | -40, 1, 3, 5, 200 47 | ``` 48 | 49 | ### Example for `reverse(int [] a)` 50 | 51 | ```java 52 | int [] a = {3,1,-40,200,5}; 53 | a = Arr.reverse(a); 54 | System.out.println(Arr.toString(a)); 55 | ``` 56 | 57 | Should print: 58 | 59 | ``` 60 | 5, 200, -40, 1, 3 61 | ``` 62 | 63 | ### Example for `join(int [] a, String glue)` 64 | 65 | ```java 66 | int [] a = {3,1,-40,200,5}; 67 | System.out.println(Arr.join(a, ": ")); 68 | System.out.println(Arr.join(a, "->")); 69 | ``` 70 | 71 | Should print: 72 | 73 | ``` 74 | 3: 1: -40: 200: 5 75 | 3->1->-40->200->5 76 | ``` 77 | 78 | ### Example for `sum(int [] a)` 79 | 80 | ```java 81 | int [] a = {1,2,3,4,5}; 82 | int b = Arr.sum(a); 83 | System.out.println(b); 84 | ``` 85 | 86 | Should print: 87 | 88 | ``` 89 | 15 90 | ``` 91 | 92 | ### Example for `range(int a, int b)` 93 | 94 | ```java 95 | int [] a = Arr.range(10, 20); 96 | System.out.println(Arr.join(a, ", ")); 97 | ``` 98 | 99 | Should print: 100 | 101 | ```java 102 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 103 | ``` 104 | 105 | ### Example for `filterOdd(int [] a)` 106 | 107 | ```java 108 | int [] a = {2,3,4,8,9,11,13,15}; 109 | int [] temp = Arr.filterOdd(a); 110 | System.out.println(Arr.join(temp, ", ")); 111 | ``` 112 | 113 | Should print: 114 | 115 | ``` 116 | 3, 9, 11, 13, 15 117 | ``` 118 | -------------------------------------------------------------------------------- /week3/2-Travel-Expenses/README.md: -------------------------------------------------------------------------------- 1 | # A program that keeps track of travel expenses 2 | 3 | In a package `com.hackbulgaria.programming51.week3` do the following: 4 | 5 | You are given the task to create a simple Java program that keeps track of the travel expenses of the company. 6 | 7 | In order to solve that problem, you will have to model: 8 | 9 | * An `Employee` class. One employee should only have a `name`. Nothing else. 10 | * A `Travel` class which represents a travel to a certain destination - country and city. 11 | * A `TravelExpense` class which represents one expense for a given travel, made by a given employee 12 | 13 | Here are full examples for each class: 14 | 15 | ## Employee 16 | 17 | Somewhere in `main`: 18 | 19 | ```java 20 | Employee rado = new Employee("Radoslav Georgiev"); 21 | System.out.println(rado.toString().equals("Radoslav Georgiev")); // true 22 | ``` 23 | 24 | ## TravelExpense 25 | 26 | A `TravelExpense` accepts type, employee and cost. 27 | 28 | Somewhere in `main`: 29 | 30 | ```java 31 | Employee ivo = new Employee("Ivaylo"); 32 | TravelExpense dinner = new TravelExpense("Dinner", ivo, 100); 33 | 34 | System.out.println(dinner.getType().equals("Dinner")); // true 35 | System.out.println(dinner.getEmployee().toString().equals("Ivaylo")); // true 36 | System.out.println(dinner.getNetCost() == 100); // true 37 | ``` 38 | 39 | ## Travel 40 | 41 | This is the fattest class. 42 | 43 | Here is how it should be initialized: 44 | 45 | ```java 46 | Travel conf = new Travel("Berlin", "Germany", 20); 47 | 48 | System.out.println(conf.getCity().equals("Berlin")); // true 49 | System.out.println(conf.getCountry().equals("Germany")); // true 50 | System.out.println(conf.getVat() == 20); // true, VAT = Value Added Tax = ДДС 51 | ``` 52 | 53 | And here is how to use that class with `TravelExpense`: 54 | 55 | ```java 56 | conf.addExpense(dinner); 57 | 58 | System.out.println(conf.totalNetCost() == 100); // true 59 | System.out.println(conf.totalCost() == 120); // VAT Included -> true 60 | ``` 61 | 62 | Also, we should be able to ask which employees have made the most expenses. 63 | 64 | **This method should return the top 2 employees, that have made the most expenses.** 65 | 66 | ```java 67 | Employee[] toFire = conf.mostExpensesMadeBy(); 68 | 69 | System.out.println(toFire[0].getName().equals("Ivaylo")); // true 70 | ``` 71 | 72 | ## Example Main 73 | 74 | Here is the example `TravelMain` class which starts it all: 75 | 76 | ```java 77 | package com.hackbulgaria.programming51.week3; 78 | 79 | public class TravelMain { 80 | 81 | public static void main(String[] args) { 82 | Employee ivo = new Employee("Ivaylo"); 83 | Employee rado = new Employee("Rado"); 84 | 85 | Travel conf = new Travel("Sofia", "Bulgaria", 20); 86 | 87 | TravelExpense dinnerRado = new TravelExpense("Dinner", rado, 50); 88 | TravelExpense dinnerIvo = new TravelExpense("Dinner", ivo, 50); 89 | 90 | conf.addExpense(dinnerRado); 91 | conf.addExpense(dinnerIvo); 92 | 93 | System.out.println(conf.totalCost()); // 120 94 | 95 | Employee[] toFire = conf.mostExpensesMadeBy(); 96 | 97 | for(Employee employee: toFire) { 98 | System.out.println(employee); 99 | } 100 | 101 | } 102 | 103 | } 104 | ``` 105 | -------------------------------------------------------------------------------- /week3/4-Integer-Vector/README.md: -------------------------------------------------------------------------------- 1 | # IntegerVector 2 | 3 | In a `com.hackbulgaria.programming51.week3` package do the following: 4 | 5 | ## Define a `class IntegerVector` that has: 6 | * public void add(int e) method for adding an element to the vector 7 | * public int size() method that returns the size of the vector 8 | * public int sum() method that returns the sum of the numbers in the vector 9 | * public void addAll(int [] n) method that adds all the elements from n to the back of the vector 10 | * public int get(int index) method that returns the value of the element on place index 11 | * public String toString() method 12 | * public void insert(int index, int data) method that inserts an element in the vector on the place marked by index 13 | * public void remove(int index) method that removes an element from the array 14 | 15 | ## Examples: 16 | 17 | ### Example for `add(int e)` and `toString()`: 18 | 19 | ```java 20 | IntegerVector v = new IntegerVector(); 21 | for (int i = 0; i < 20; i++) { 22 | v.add(i); 23 | } 24 | System.out.println(v); 25 | ``` 26 | 27 | Should print: 28 | 29 | ``` 30 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 31 | ``` 32 | 33 | ### Example for `size()`: 34 | 35 | ```java 36 | IntegerVector v = new IntegerVector(); 37 | for (int i = 0; i < 20; i++) { 38 | v.add(i); 39 | } 40 | v.add(50); 41 | System.out.println(v.size()); 42 | ``` 43 | 44 | Should print: 45 | 46 | ``` 47 | 21 48 | ``` 49 | 50 | ### Example for `sum()`: 51 | 52 | ```java 53 | IntegerVector v = new IntegerVector(); 54 | for (int i = 0; i < 20; i++) { 55 | v.add(i); 56 | } 57 | System.out.println(v.sum()); 58 | ``` 59 | 60 | Should print: 61 | 62 | ``` 63 | 190 64 | ``` 65 | 66 | ### Example for `addAll(int [] n)`: 67 | 68 | ```java 69 | IntegerVector v = new IntegerVector(); 70 | for (int i = 0; i < 20; i++) { 71 | v.add(i); 72 | } 73 | 74 | int [] b = new int[10]; 75 | for (int i = 0; i < 10; i++) { 76 | b[i] = i; 77 | } 78 | v.addAll(b); 79 | System.out.println(v); 80 | ``` 81 | 82 | Should print: 83 | 84 | ``` 85 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 9 86 | ``` 87 | 88 | ### Example for `get(int index)`: 89 | 90 | ```java 91 | IntegerVector v = new IntegerVector(); 92 | for (int i = 0; i < 20; i++) { 93 | v.add(i); 94 | } 95 | 96 | System.out.println(v.get(3)); 97 | System.out.println(v.get(9)); 98 | 99 | ``` 100 | 101 | Should print: 102 | 103 | ``` 104 | 3 105 | 9 106 | ``` 107 | 108 | ### Example for `insert(int index, int data)` 109 | 110 | ```java 111 | IntegerVector v = new IntegerVector(); 112 | for (int i = 0; i < 10; i++) { 113 | v.add(i); 114 | } 115 | System.out.println(v); 116 | v.insert(3,10); 117 | System.out.println(v); 118 | ``` 119 | 120 | Should print: 121 | 122 | ``` 123 | 0 1 2 3 4 5 6 7 8 9 124 | 0 1 2 10 3 4 5 6 7 8 9 125 | ``` 126 | 127 | ### Example for `remove(int index)` 128 | 129 | ```java 130 | IntegerVector v = new IntegerVector(); 131 | for (int i = 0; i < 10; i++) { 132 | v.add(i); 133 | } 134 | System.out.println(v); 135 | v.remove(3); 136 | System.out.println(v); 137 | ``` 138 | 139 | Should print: 140 | 141 | ``` 142 | 0 1 2 3 4 5 6 7 8 9 143 | 0 1 2 4 5 6 7 8 9 144 | ``` 145 | -------------------------------------------------------------------------------- /week3/5-Integer-Vector-Static/README.md: -------------------------------------------------------------------------------- 1 | # Ingeger-Vector-Static 2 | 3 | In a `com.hackbulgaria.programming51.week3` package do the following: 4 | 5 | ## Define a `class IntegerVectorStatic` where you 6 | 7 | ### Define the following 8 | 9 | * `public static String toString(IntegerVector v)` method that recieves an integer array and returns a string of the array, separated by "," 10 | * `public static void sort(IntegerVector v)` method that recieves an array of int and sorts it 11 | * `public static IntegerVector reverse(IntegerVector v)` method that recieves an integer array and returns the reversed array 12 | * `public static String join(IntegerVector v, String glue)` method that recieves an array of int and a string and returns a String of the array elements with 'glue' between them 13 | * `public static IntegerVector range(int a, int b)` that returns an array of int with elements in the range between a and b 14 | * `public static IntegerVector filterOdd(IntegerVector v)` method that returns an array with only the Odd numbers from 'v' 15 | 16 | ## Examples 17 | 18 | 19 | ### Example for `IntegerVectorStatic.toString(IntegerVector v)` 20 | 21 | ```java 22 | int [] a = {1,2,3,4,5}; 23 | IntegerVector v = new IntegerVector(); 24 | v.addAll(a); 25 | System.out.println(IntegerVectorStatic.toString(v)); 26 | ``` 27 | 28 | Should print: 29 | 30 | ``` 31 | 1, 2, 3, 4, 5 32 | ``` 33 | 34 | ### Example for `IntegerVectorStatic.sort(IntegerVector v)` 35 | 36 | ```java 37 | int [] a = {3,1,-40,200,5}; 38 | IntegerVector v = new IntegerVector(); 39 | v.addAll(a); 40 | IntegerVectorStatic.sort(v); 41 | System.out.println(IntegerVectorStatic.toString(v)); 42 | ``` 43 | 44 | Should print: 45 | 46 | ``` 47 | -40, 1, 3, 5, 200 48 | ``` 49 | 50 | ### Example for `reverse(IntegerVector v)` 51 | 52 | ```java 53 | int [] a = {3,1,-40,200,5}; 54 | IntegerVector v = new IntegerVector(); 55 | IntegerVector temp = IntegerVectorStatic.reverse(v); 56 | System.out.println(IntegerVectorStatic.toString(temp)); 57 | ``` 58 | 59 | Should print: 60 | 61 | ``` 62 | 5, 200, -40, 1, 3 63 | ``` 64 | 65 | ### Example for `join(IntegerVector v, String glue)` 66 | 67 | ```java 68 | int [] a = {3,1,-40,200,5}; 69 | IntegerVector v = new IntegerVector(); 70 | v.addAll(a); 71 | System.out.println(IntegerVectorStatic.join(v,": ")); 72 | System.out.println(IntegerVectorStatic.join(v,"->")); 73 | ``` 74 | 75 | Should print: 76 | 77 | ``` 78 | 3: 1: -40: 200: 5 79 | 3->1->-40->200->5 80 | ``` 81 | 82 | ### Example for `sum(IntegerVector v)` 83 | 84 | ```java 85 | int [] a = {1,2,3,4,5}; 86 | IntegerVector v = new IntegerVector(); 87 | v.addAll(a); 88 | System.out.println(IntegerVectorStatic.sum(v)); 89 | ``` 90 | 91 | Should print: 92 | 93 | ``` 94 | 15 95 | ``` 96 | 97 | ### Example for `range(int a, int b)` 98 | 99 | ```java 100 | IntegerVector v = new IntegerVector(); 101 | v = IntegerStatic.range(10,20); 102 | System.out.println(IntegerVectorStatic.toString(v)); 103 | ``` 104 | 105 | Should print: 106 | 107 | ```java 108 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 109 | ``` 110 | 111 | ### Example for `filterOdd(IntegerVector v)` 112 | 113 | ```java 114 | int [] a = {2,3,4,8,9,11,13,15}; 115 | IntegerVector v = new IntegerVector(); 116 | v.addAll(a); 117 | IntegerVector temp = IntegerVectorStatic.filterOdd(v); 118 | System.out.println(temp); 119 | ``` 120 | 121 | Should print: 122 | 123 | ``` 124 | 3, 9, 11, 13, 15 125 | ``` 126 | -------------------------------------------------------------------------------- /week3/README.md: -------------------------------------------------------------------------------- 1 | # Arrays & Vectors - our first data structure 2 | 3 | ## Static methods 4 | 5 | First, we talked about static methods. We recommend looking at the following materials: 6 | 7 | * 8 | * 9 | 10 | ## Vectors 11 | 12 | After this, we started looking at the Vector data structure - a way to overcome the limitations that we have with arrays. 13 | 14 | Vectors are fundamental data structures. As materials, you can take a look at: 15 | 16 | * [The slides about Vectors](http://slides.com/hackbulgaria/deck-21-22) 17 | * [Implementation of Vector with generic types in Java](https://github.com/HackBulgaria/Programming51-1/blob/master/week4/materials/Vector.java) 18 | 19 | -------------------------------------------------------------------------------- /week3/materials/Arr.java: -------------------------------------------------------------------------------- 1 | package week3; 2 | 3 | public class Arr { 4 | 5 | public static void print(int[] a) { 6 | for (int i = 0; i < a.length; i++) { 7 | System.out.print(a[i] + ", "); 8 | } 9 | System.out.println(); 10 | } 11 | 12 | public static int sum(int[] a) { 13 | int sum = 0; 14 | for (int i = 0; i < a.length; i++) { 15 | sum += a[i]; 16 | } 17 | return sum; 18 | } 19 | 20 | public static int[] messUp(int[] a) { 21 | int[] temp = Arr.copy(a); 22 | for (int i = 0; i < a.length; i++) { 23 | if (i % 2 == 0) { 24 | temp[i] = 0; 25 | } 26 | } 27 | return temp; 28 | } 29 | 30 | public static int[] copy(int[] a) { 31 | int[] temp = new int[a.length]; 32 | for (int i = 0; i < a.length; i++) { 33 | temp[i] = a[i]; 34 | } 35 | return temp; 36 | } 37 | 38 | public static int[] sort(int[] a) { 39 | int[] result = copy(a); 40 | for (int i = 0; i < result.length; i++) { 41 | 42 | int index = i; 43 | for (int j = i; j < result.length; j++) { 44 | if (result[j] < result[index]) { 45 | index = j; 46 | } 47 | } 48 | 49 | int temp = result[i]; 50 | result[i] = result[index]; 51 | result[index] = temp; 52 | } 53 | 54 | return result; 55 | } 56 | 57 | public static void bubbleSort(int[] a) { 58 | for(int i=0;i getAlbums()` that returns an vector of all the **different** album names in the Music Organiser 27 | * method `public int getAlbumsCount()` that returns the number of **different** album names in the Music Organiser 28 | * method `public Vector getAlbumSongs(String albumName)` that returns the songs in an album 29 | * method `public Vector getAlbumsAndSongs()` that returns an vector of all the **different** album names in the Music Organiser, and the number of songs in each album concatenated in one string for each album name. **Check the example below** 30 | 31 | ## Examples 32 | 33 | ### Example for `addSong()` 34 | 35 | ```java 36 | MusicOrganiser mo = new MusicOrganiser(); 37 | 38 | mo.addSong(new Song("The real Slim Shady", "Eminem", 213, "The Marshall Mathers LP")); 39 | 40 | System.out.println(mo); 41 | ``` 42 | 43 | Should print: 44 | 45 | ``` 46 | The real Slim Shady Eminem 213 The Marshall Mathers LP 47 | ``` 48 | 49 | ### Example for `getCount()` 50 | 51 | ```java 52 | MusicOrganiser mo = new MusicOrganiser(); 53 | 54 | mo.addSong(new Song("The real Slim Shady", "Eminem", 213, "The Marshall Mathers LP")); 55 | mo.addSong(new Song("The Way I Am", "Eminem", 183, "The Marshall Mathers LP")); 56 | mo.addSong(new Song("When I'm Gone", "Eminem", 256, "Curtain Call: The Hits")); 57 | mo.addSong(new Song("Lose Yourself", "Eminem", 221, "Curtain Call: The Hits")); 58 | 59 | System.out.println(mo.getCount()); 60 | ``` 61 | 62 | Should print: 63 | 64 | ``` 65 | 4 66 | ``` 67 | 68 | ### Example for `getAlbums()` 69 | 70 | ```java 71 | MusicOrganiser mo = new MusicOrganiser(); 72 | mo.addSong(new Song("The real Slim Shady", "Eminem", 213, "The Marshall Mathers LP")); 73 | mo.addSong(new Song("The Way I Am", "Eminem", 183, "The Marshall Mathers LP")); 74 | mo.addSong(new Song("When I'm Gone", "Eminem", 256, "Curtain Call: The Hits")); 75 | mo.addSong(new Song("Lose Yourself", "Eminem", 221, "Curtain Call: The Hits")); 76 | 77 | Vector albums = mo.getAlbums(); 78 | 79 | for(int i = 0; i < albums.size(); i++) { 80 | System.out.println(albums.get(i)); 81 | } 82 | ``` 83 | 84 | Should print: 85 | 86 | ``` 87 | The Marshall Mathers LP 88 | Curtain Call: The Hits 89 | ``` 90 | 91 | ### Example for `getAlbumsCount()` 92 | 93 | ```java 94 | MusicOrganiser mo = new MusicOrganiser(); 95 | 96 | mo.addSong(new Song("The real Slim Shady", "Eminem", 213, "The Marshall Mathers LP")); 97 | mo.addSong(new Song("The Way I Am", "Eminem", 183, "The Marshall Mathers LP")); 98 | mo.addSong(new Song("When I'm Gone", "Eminem", 256, "Curtain Call: The Hits")); 99 | mo.addSong(new Song("Lose Yourself", "Eminem", 221, "Curtain Call: The Hits")); 100 | 101 | System.out.println(mo.getAlbumsCount()); 102 | ``` 103 | 104 | Should print: 105 | 106 | ``` 107 | 2 108 | ``` 109 | 110 | ### Example for `getAlbumSongs()` 111 | 112 | ```java 113 | MusicOrganiser mo = new MusicOrganiser(); 114 | 115 | mo.addSong(new Song("The real Slim Shady", "Eminem", 213, "The Marshall Mathers LP")); 116 | mo.addSong(new Song("The Way I Am", "Eminem", 183, "The Marshall Mathers LP")); 117 | mo.addSong(new Song("When I'm Gone", "Eminem", 256, "Curtain Call: The Hits")); 118 | mo.addSong(new Song("Lose Yourself", "Eminem", 221, "Curtain Call: The Hits")); 119 | 120 | Vector songs = mo.getAlbumSongs("The Marshall Mathers LP"); 121 | 122 | for(int i = 0; i < songs.size(); i++) { 123 | System.out.println(songs.get(i)); 124 | } 125 | ``` 126 | 127 | Should print: 128 | 129 | ``` 130 | The real Slim Shady Eminem 213 The Marshall Mathers LP 131 | The Way I Am Eminem 183 The Marshall Mathers LP 132 | ``` 133 | 134 | ### Example for `getAlbumsAndSongs()` 135 | 136 | ```java 137 | MusicOrganiser mo = new MusicOrganiser(); 138 | 139 | mo.addSong(new Song("The real Slim Shady", "Eminem", 213, "The Marshall Mathers LP")); 140 | mo.addSong(new Song("The Way I Am", "Eminem", 183, "The Marshall Mathers LP")); 141 | mo.addSong(new Song("When I'm Gone", "Eminem", 256, "Curtain Call: The Hits")); 142 | mo.addSong(new Song("Lose Yourself", "Eminem", 221, "Curtain Call: The Hits")); 143 | 144 | Vector songs = mo.getAlbumsAndSongs(); 145 | 146 | for(int i = 0; i < songs.size(); i++) { 147 | System.out.println(songs.get(i)); 148 | } 149 | ``` 150 | 151 | Should print: 152 | 153 | ``` 154 | The Marshall Mathers LP(2) 155 | Curtain Call: The Hits(2) 156 | ``` 157 | -------------------------------------------------------------------------------- /week4/2-Birthday-Ranges/BirthdayRanges.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Vector; 3 | 4 | public class BirthdayRanges { 5 | public static class IntPair { 6 | public int start; 7 | public int end; 8 | } 9 | 10 | public static Vector birthdaysCount(Vector birthdays, 11 | Vector ranges) { 12 | Vector result = new Vector(); 13 | 14 | for(IntPair range: ranges) { 15 | int count = 0; 16 | 17 | for(int bday: birthdays) { 18 | if(bday >= range.start && bday <= range.end) { 19 | count += 1; 20 | } 21 | } 22 | 23 | result.add(count); 24 | } 25 | 26 | return result; 27 | } 28 | 29 | public static void main(String[] args) { 30 | Vector birthdays = new Vector(); 31 | Vector ranges = new Vector(); 32 | 33 | Scanner scanner = new Scanner(System.in); 34 | 35 | int bdaysCount = scanner.nextInt(); 36 | int rangesCount = scanner.nextInt(); 37 | 38 | for (int i = 0; i < bdaysCount; i++) { 39 | birthdays.add(scanner.nextInt()); 40 | } 41 | 42 | for (int i = 0; i < rangesCount; i++) { 43 | IntPair pair = new IntPair(); 44 | 45 | pair.start = scanner.nextInt(); 46 | pair.end = scanner.nextInt(); 47 | 48 | ranges.add(pair); 49 | } 50 | 51 | Vector result = birthdaysCount(birthdays, ranges); 52 | 53 | for (int count : result) { 54 | System.out.println(count); 55 | } 56 | 57 | } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /week4/2-Birthday-Ranges/README.md: -------------------------------------------------------------------------------- 1 | # Birthday Ranges 2 | 3 | We are given the following input: 4 | 5 | * A vector of people's birthdays (range from 0 to 365). Each value is the day of the year when that person was born. 6 | * A vector of ranges (one range is a pair of two numbers - start and end). Each pair will be between 0 and 365. 7 | 8 | We want to calculate, for each pair, how many people are born in that range (between start and end inclusive) 9 | 10 | 11 | For example: 12 | 13 | ``` 14 | Birthdays - [5, 10, 6, 7, 3, 4, 5, 11, 21, 300, 15] 15 | Ranges - [(4, 9), (6, 7), (200, 225), (300, 365)] 16 | ``` 17 | 18 | Will give the result: 19 | 20 | ``` 21 | [5, 2, 0, 1] 22 | ``` 23 | 24 | * As we can see, betweeh 4 and 9, inclusive, there are 5 people with birthdays - 5, 6, 7, 4, 5 25 | * Between 300 and 365 there is exatly one birthday - 300. 26 | 27 | 28 | In a class called `BirthdayRanges` create a static method: 29 | 30 | ```java 31 | public class BirthdayRanges { 32 | 33 | public static Vector birthdaysCount(Vector birthdays, Vector ranges) { 34 | // implementation 35 | } 36 | 37 | } 38 | ``` 39 | 40 | ## IntPair class 41 | 42 | Here is the implementation: 43 | 44 | ```java 45 | public class IntPair { 46 | public int start; 47 | public int end; 48 | } 49 | ``` 50 | 51 | ## Examples 52 | 53 | **Here is a boilerplate class ready to take console input:** 54 | 55 | ```java 56 | package com.hackbulgaria.programming51.week4; 57 | 58 | import java.util.Scanner; 59 | import java.util.Vector; 60 | 61 | public class BirthdayRanges { 62 | // Since we can include only 1 file in maycamp, have a static class 63 | public static class IntPair { 64 | public int start; 65 | public int end; 66 | } 67 | 68 | public static Vector birthdaysCount(Vector birthdays, 69 | Vector ranges) { 70 | Vector result = new Vector(); 71 | 72 | return result; 73 | } 74 | 75 | public static void main(String[] args) { 76 | Vector birthdays = new Vector(); 77 | Vector ranges = new Vector(); 78 | 79 | Scanner scanner = new Scanner(System.in); 80 | 81 | int bdaysCount = scanner.nextInt(); 82 | int rangesCount = scanner.nextInt(); 83 | 84 | for (int i = 0; i < bdaysCount; i++) { 85 | birthdays.add(scanner.nextInt()); 86 | } 87 | 88 | for (int i = 0; i < rangesCount; i++) { 89 | IntPair pair = new IntPair(); 90 | 91 | pair.start = scanner.nextInt(); 92 | pair.end = scanner.nextInt(); 93 | 94 | ranges.add(pair); 95 | } 96 | 97 | Vector result = birthdaysCount(birthdays, ranges); 98 | 99 | for (int count : result) { 100 | System.out.println(count); 101 | } 102 | 103 | } 104 | } 105 | 106 | ``` 107 | 108 | And now, for input: (We read `N` = number of birthdays and `M` = number of ranges as the first two integers of the input) 109 | 110 | ``` 111 | 11 4 112 | 5 10 6 7 3 4 5 11 21 300 15 113 | 4 9 114 | 6 7 115 | 200 225 116 | 300 365 117 | ``` 118 | 119 | We should get output: 120 | 121 | ``` 122 | Result is: 123 | 5 124 | 2 125 | 0 126 | 1 127 | ``` 128 | 129 | --- 130 | 131 | For input: 132 | 133 | ``` 134 | 5 5 135 | 1 2 3 4 5 136 | 1 2 137 | 1 3 138 | 1 4 139 | 1 5 140 | 4 6 141 | ``` 142 | 143 | 144 | We should get output: 145 | 146 | ``` 147 | Result is: 148 | 2 149 | 3 150 | 4 151 | 5 152 | 2 153 | ``` 154 | -------------------------------------------------------------------------------- /week4/2-Birthday-Ranges/maycamp/01.ans: -------------------------------------------------------------------------------- 1 | 5 2 | 2 3 | 0 4 | 1 5 | -------------------------------------------------------------------------------- /week4/2-Birthday-Ranges/maycamp/01.in: -------------------------------------------------------------------------------- 1 | 11 4 2 | 5 10 6 7 3 4 5 11 21 300 15 3 | 4 9 4 | 6 7 5 | 200 225 6 | 300 365 7 | -------------------------------------------------------------------------------- /week4/2-Birthday-Ranges/maycamp/02.ans: -------------------------------------------------------------------------------- 1 | 8 2 | 10 3 | 5 4 | 12 5 | 20 6 | 0 7 | 20 8 | 5 9 | 5 10 | 8 11 | -------------------------------------------------------------------------------- /week4/2-Birthday-Ranges/maycamp/02.in: -------------------------------------------------------------------------------- 1 | 20 10 2 | 5 15 9 5 2 1 12 6 1 3 9 2 16 18 2 15 15 19 6 16 3 | 1 5 4 | 2 9 5 | 3 7 6 | 4 16 7 | 1 19 8 | 200 300 9 | 1 150 10 | 1 2 11 | 15 16 12 | 2 6 13 | -------------------------------------------------------------------------------- /week4/2-Birthday-Ranges/maycamp/03.ans: -------------------------------------------------------------------------------- 1 | 58 2 | 0 3 | 13 4 | 13 5 | 31 6 | 0 7 | 0 8 | 104 9 | 120 10 | 30 11 | 37 12 | 13 13 | 0 14 | 133 15 | 37 16 | 0 17 | 141 18 | 19 19 | 80 20 | 141 21 | 58 22 | 0 23 | 29 24 | 37 25 | 0 26 | 0 27 | 48 28 | 104 29 | 51 30 | 141 31 | 120 32 | 18 33 | 29 34 | 200 35 | 0 36 | 91 37 | 30 38 | 0 39 | 24 40 | 13 41 | 23 42 | 37 43 | 104 44 | 0 45 | 0 46 | 0 47 | 0 48 | 0 49 | 194 50 | 164 51 | 0 52 | 30 53 | 18 54 | 32 55 | 104 56 | 128 57 | 30 58 | 48 59 | 58 60 | 106 61 | 31 62 | 0 63 | 135 64 | 93 65 | 0 66 | 58 67 | 49 68 | 72 69 | 104 70 | 81 71 | 0 72 | 0 73 | 82 74 | 29 75 | 48 76 | 58 77 | 91 78 | 0 79 | 30 80 | 13 81 | 48 82 | 0 83 | 85 84 | 27 85 | 13 86 | 141 87 | 30 88 | 158 89 | 138 90 | 109 91 | 194 92 | 104 93 | 0 94 | 184 95 | 111 96 | 109 97 | 24 98 | 42 99 | 133 100 | 13 101 | 36 102 | 141 103 | 0 104 | 13 105 | 0 106 | 0 107 | 59 108 | 18 109 | 0 110 | 83 111 | 31 112 | 92 113 | 109 114 | 16 115 | 93 116 | 104 117 | 121 118 | 93 119 | 0 120 | 23 121 | 48 122 | 0 123 | 151 124 | 151 125 | 0 126 | 103 127 | 0 128 | 200 129 | 13 130 | 0 131 | 12 132 | 0 133 | 36 134 | 50 135 | 56 136 | 52 137 | 23 138 | 0 139 | 128 140 | 82 141 | 174 142 | 0 143 | 59 144 | 128 145 | 58 146 | 94 147 | 0 148 | 0 149 | 103 150 | 0 151 | 13 152 | 58 153 | 13 154 | 12 155 | 133 156 | 0 157 | 93 158 | 164 159 | 109 160 | 42 161 | 58 162 | 103 163 | 0 164 | 92 165 | 0 166 | 48 167 | 58 168 | 0 169 | 194 170 | 129 171 | 47 172 | 74 173 | 22 174 | 184 175 | 83 176 | 22 177 | 106 178 | 72 179 | 0 180 | 0 181 | 0 182 | 33 183 | 36 184 | 93 185 | 0 186 | 0 187 | 49 188 | 194 189 | 59 190 | 36 191 | 0 192 | 0 193 | 0 194 | 61 195 | 0 196 | 0 197 | 33 198 | 30 199 | 43 200 | 24 201 | -------------------------------------------------------------------------------- /week4/2-Birthday-Ranges/maycamp/03.in: -------------------------------------------------------------------------------- 1 | 200 200 2 | 10 15 9 20 5 3 13 15 16 7 17 20 3 5 5 14 2 19 15 7 13 7 15 14 20 14 2 2 9 3 17 4 20 18 14 15 14 20 19 10 5 8 6 18 14 6 2 20 8 19 11 5 1 14 19 20 15 10 19 20 8 15 12 3 9 9 15 6 11 15 11 5 19 20 6 5 4 8 5 17 6 17 16 6 9 3 8 11 3 17 11 19 10 20 17 16 13 18 9 4 17 4 4 2 7 4 19 15 8 14 19 1 19 3 8 19 19 6 7 7 4 11 5 5 15 1 6 20 13 17 14 18 7 15 2 15 12 15 11 2 3 20 8 19 4 11 5 8 16 16 6 19 3 9 8 16 6 11 14 19 13 16 12 8 10 9 16 1 19 9 1 11 8 19 17 13 13 9 5 17 18 16 2 4 8 18 2 13 7 17 5 17 20 13 9 5 2 1 16 3 3 | 16 24 4 | 22 25 5 | 20 26 6 | 20 25 7 | 6 8 8 | 21 26 9 | 25 26 10 | 11 26 11 | 9 20 12 | 19 25 13 | 10 14 14 | 20 21 15 | 25 26 16 | 8 24 17 | 7 10 18 | 25 26 19 | 7 26 20 | 3 4 21 | 1 8 22 | 7 25 23 | 16 25 24 | 22 26 25 | 8 10 26 | 7 10 27 | 24 26 28 | 23 26 29 | 17 24 30 | 11 24 31 | 3 7 32 | 7 20 33 | 9 25 34 | 17 18 35 | 8 10 36 | 1 24 37 | 24 25 38 | 13 23 39 | 19 20 40 | 21 22 41 | 14 15 42 | 20 24 43 | 18 19 44 | 10 14 45 | 11 25 46 | 24 25 47 | 23 25 48 | 21 25 49 | 21 23 50 | 22 26 51 | 2 20 52 | 2 18 53 | 21 22 54 | 19 20 55 | 17 18 56 | 11 14 57 | 11 20 58 | 7 19 59 | 19 23 60 | 17 24 61 | 16 23 62 | 1 11 63 | 6 8 64 | 25 26 65 | 5 18 66 | 7 16 67 | 24 25 68 | 16 25 69 | 1 5 70 | 15 26 71 | 11 23 72 | 12 19 73 | 21 26 74 | 22 26 75 | 14 24 76 | 8 10 77 | 17 25 78 | 16 21 79 | 13 25 80 | 25 26 81 | 19 21 82 | 20 23 83 | 17 22 84 | 22 25 85 | 8 16 86 | 10 13 87 | 20 26 88 | 7 25 89 | 19 21 90 | 2 17 91 | 6 19 92 | 10 22 93 | 2 26 94 | 11 22 95 | 24 26 96 | 3 24 97 | 7 18 98 | 10 25 99 | 5 6 100 | 8 12 101 | 8 26 102 | 20 23 103 | 18 25 104 | 7 23 105 | 25 26 106 | 20 26 107 | 25 26 108 | 24 26 109 | 15 19 110 | 17 18 111 | 21 24 112 | 4 12 113 | 6 8 114 | 4 13 115 | 10 20 116 | 1 2 117 | 3 12 118 | 11 22 119 | 6 18 120 | 7 16 121 | 23 25 122 | 18 19 123 | 17 23 124 | 25 26 125 | 6 20 126 | 6 20 127 | 24 26 128 | 8 18 129 | 24 26 130 | 1 21 131 | 20 25 132 | 23 26 133 | 12 13 134 | 24 26 135 | 18 22 136 | 7 12 137 | 11 16 138 | 14 18 139 | 18 19 140 | 21 23 141 | 1 14 142 | 14 24 143 | 4 24 144 | 22 25 145 | 15 19 146 | 1 14 147 | 16 25 148 | 12 26 149 | 21 23 150 | 23 24 151 | 8 18 152 | 25 26 153 | 20 26 154 | 16 25 155 | 20 26 156 | 12 13 157 | 8 26 158 | 21 26 159 | 5 14 160 | 2 18 161 | 1 12 162 | 15 18 163 | 16 24 164 | 6 16 165 | 22 24 166 | 4 13 167 | 24 25 168 | 17 23 169 | 16 22 170 | 24 26 171 | 2 26 172 | 5 17 173 | 6 10 174 | 11 18 175 | 12 14 176 | 3 24 177 | 7 15 178 | 11 13 179 | 1 11 180 | 9 16 181 | 22 26 182 | 21 25 183 | 23 25 184 | 13 15 185 | 18 25 186 | 3 12 187 | 25 26 188 | 24 26 189 | 1 5 190 | 2 22 191 | 15 19 192 | 18 20 193 | 21 23 194 | 22 26 195 | 25 26 196 | 13 18 197 | 21 26 198 | 25 26 199 | 13 15 200 | 19 23 201 | 13 16 202 | 8 9 203 | -------------------------------------------------------------------------------- /week4/2-Birthday-Ranges/maycamp/04.ans: -------------------------------------------------------------------------------- 1 | 195 2 | 1777 3 | 0 4 | 0 5 | 1237 6 | 0 7 | 360 8 | 894 9 | 1126 10 | 137 11 | 0 12 | 1072 13 | 0 14 | 566 15 | 156 16 | 327 17 | 0 18 | 79 19 | 0 20 | 59 21 | 118 22 | 1430 23 | 0 24 | 0 25 | 475 26 | 0 27 | 0 28 | 694 29 | 14 30 | 0 31 | 0 32 | 598 33 | 378 34 | 1805 35 | 0 36 | 115 37 | 1407 38 | 1302 39 | 697 40 | 1337 41 | 0 42 | 0 43 | 300 44 | 470 45 | 0 46 | 663 47 | 1306 48 | 211 49 | 486 50 | 543 51 | 175 52 | 0 53 | 156 54 | 0 55 | 33 56 | 119 57 | 1624 58 | 787 59 | 971 60 | 228 61 | 1212 62 | 2000 63 | 0 64 | 1126 65 | 0 66 | 894 67 | 72 68 | 344 69 | 238 70 | 630 71 | 0 72 | 0 73 | 1601 74 | 1407 75 | 682 76 | 0 77 | 0 78 | 0 79 | 77 80 | 1824 81 | 464 82 | 861 83 | 96 84 | 1368 85 | 32 86 | 694 87 | 367 88 | 2000 89 | 712 90 | 639 91 | 1642 92 | 454 93 | 1175 94 | 582 95 | 1353 96 | 0 97 | 0 98 | 1233 99 | 0 100 | 0 101 | 1624 102 | 458 103 | 323 104 | 79 105 | 367 106 | 833 107 | 0 108 | 703 109 | 861 110 | 1744 111 | 0 112 | 175 113 | 155 114 | 259 115 | 0 116 | 585 117 | 582 118 | 1498 119 | 0 120 | 914 121 | 0 122 | 746 123 | 542 124 | 582 125 | 0 126 | 1178 127 | 516 128 | 582 129 | 0 130 | 414 131 | 617 132 | 97 133 | 861 134 | 348 135 | 1430 136 | 1733 137 | 811 138 | 0 139 | 0 140 | 828 141 | 884 142 | 1624 143 | 1140 144 | 268 145 | 1104 146 | 200 147 | 1087 148 | 0 149 | 0 150 | 717 151 | 1104 152 | 0 153 | 0 154 | 0 155 | 1087 156 | 0 157 | 948 158 | 581 159 | 304 160 | 0 161 | 0 162 | 0 163 | 1148 164 | 0 165 | 948 166 | 0 167 | 957 168 | 0 169 | 342 170 | 0 171 | 85 172 | 0 173 | 103 174 | 0 175 | 1013 176 | 1077 177 | 0 178 | 345 179 | 124 180 | 1305 181 | 602 182 | 103 183 | 1388 184 | 640 185 | 1426 186 | 310 187 | 461 188 | 486 189 | 552 190 | 59 191 | 1407 192 | 305 193 | 0 194 | 0 195 | 282 196 | 0 197 | 621 198 | 1521 199 | 742 200 | 0 201 | 597 202 | 364 203 | 1083 204 | 1362 205 | 599 206 | 327 207 | 0 208 | 0 209 | 215 210 | 367 211 | 345 212 | 507 213 | 0 214 | 0 215 | 711 216 | 0 217 | 1279 218 | 1126 219 | 0 220 | 79 221 | 0 222 | 127 223 | 599 224 | 0 225 | 0 226 | 0 227 | 195 228 | 118 229 | 1061 230 | 305 231 | 1214 232 | 1321 233 | 643 234 | 228 235 | 0 236 | 443 237 | 949 238 | 520 239 | 543 240 | 1777 241 | 0 242 | 459 243 | 0 244 | 0 245 | 787 246 | 0 247 | 1576 248 | 703 249 | 404 250 | 1147 251 | 0 252 | 502 253 | 0 254 | 0 255 | 844 256 | 404 257 | 259 258 | 33 259 | 703 260 | 540 261 | 1545 262 | 136 263 | 1029 264 | 0 265 | 0 266 | 605 267 | 0 268 | 368 269 | 475 270 | 0 271 | 0 272 | 540 273 | 137 274 | 0 275 | 190 276 | 676 277 | 367 278 | 33 279 | 948 280 | 376 281 | 135 282 | 105 283 | 446 284 | 0 285 | 1087 286 | 421 287 | 1777 288 | 1562 289 | 769 290 | 612 291 | 259 292 | 0 293 | 0 294 | 295 295 | 421 296 | 0 297 | 1315 298 | 844 299 | 0 300 | 195 301 | 387 302 | 0 303 | 894 304 | 0 305 | 0 306 | 1624 307 | 0 308 | 123 309 | 828 310 | 446 311 | 0 312 | 545 313 | 760 314 | 79 315 | 0 316 | 861 317 | 0 318 | 0 319 | 1719 320 | 0 321 | 0 322 | 228 323 | 271 324 | 762 325 | 861 326 | 0 327 | 593 328 | 599 329 | 325 330 | 0 331 | 1601 332 | 1541 333 | 1017 334 | 1867 335 | 363 336 | 486 337 | 463 338 | 103 339 | 0 340 | 175 341 | 0 342 | 0 343 | 87 344 | 0 345 | 137 346 | 1073 347 | 0 348 | 0 349 | 1799 350 | 747 351 | 703 352 | 502 353 | 1337 354 | 1368 355 | 1190 356 | 1549 357 | 124 358 | 0 359 | 1109 360 | 0 361 | 14 362 | 1576 363 | 1268 364 | 1624 365 | 345 366 | 282 367 | 1126 368 | 1128 369 | 0 370 | 829 371 | 367 372 | 0 373 | 0 374 | 663 375 | 1430 376 | 0 377 | 1363 378 | 156 379 | 507 380 | 0 381 | 599 382 | 137 383 | 155 384 | 0 385 | 0 386 | 0 387 | 0 388 | 1560 389 | 0 390 | 0 391 | 1315 392 | 250 393 | 0 394 | 1818 395 | 879 396 | 124 397 | 543 398 | 420 399 | 304 400 | 0 401 | 543 402 | 0 403 | 1449 404 | 0 405 | 1008 406 | 582 407 | 1279 408 | 0 409 | 1945 410 | 1061 411 | 1175 412 | 1254 413 | 1642 414 | 894 415 | 103 416 | 0 417 | 1843 418 | 582 419 | 1126 420 | 0 421 | 139 422 | 0 423 | 72 424 | 463 425 | 1474 426 | 891 427 | 0 428 | 0 429 | 1279 430 | 0 431 | 1072 432 | 0 433 | 127 434 | 858 435 | 0 436 | 757 437 | 304 438 | 0 439 | 139 440 | 0 441 | 404 442 | 0 443 | 1262 444 | 0 445 | 1966 446 | 0 447 | 266 448 | 520 449 | 0 450 | 304 451 | 1279 452 | 246 453 | 1087 454 | 1902 455 | 1257 456 | 103 457 | 741 458 | 0 459 | 345 460 | 0 461 | 0 462 | 103 463 | 0 464 | 1175 465 | 423 466 | 0 467 | 1476 468 | 0 469 | 1122 470 | 0 471 | 137 472 | 126 473 | 917 474 | 834 475 | 0 476 | 0 477 | 914 478 | 0 479 | 641 480 | 0 481 | 1642 482 | 0 483 | 14 484 | 716 485 | 0 486 | 599 487 | 144 488 | 520 489 | 323 490 | 0 491 | 0 492 | 543 493 | 520 494 | 844 495 | 0 496 | 374 497 | 89 498 | 0 499 | 954 500 | 0 501 | -------------------------------------------------------------------------------- /week4/2-Birthday-Ranges/maycamp/04.in: -------------------------------------------------------------------------------- 1 | 2000 500 2 | 58 92 57 58 13 33 14 72 74 74 20 53 49 91 78 4 64 76 77 83 15 64 34 95 83 28 36 67 43 23 40 84 88 81 52 37 43 98 25 30 97 88 28 42 39 12 84 29 21 25 6 51 91 65 29 79 90 28 55 47 96 22 84 52 44 49 53 41 96 59 87 87 47 76 1 39 5 11 47 19 41 85 1 81 14 71 12 98 13 18 23 95 38 36 68 46 13 20 40 54 11 20 12 90 73 86 94 35 60 73 3 93 90 49 82 33 28 10 61 68 64 25 19 30 4 87 14 99 8 50 77 82 37 98 48 25 50 88 10 80 24 93 52 73 92 68 21 18 83 16 90 41 10 1 70 25 67 13 36 22 100 20 8 79 29 15 75 10 39 20 35 68 92 47 62 85 64 87 70 74 38 7 77 5 7 98 90 72 42 62 84 90 14 41 93 23 84 45 37 70 96 45 40 88 64 55 30 90 10 81 97 15 27 43 36 87 77 25 70 51 18 56 41 18 1 86 76 8 69 72 79 33 64 20 41 12 54 50 22 88 61 86 86 18 55 26 6 80 53 54 86 94 91 37 18 55 38 85 49 87 29 87 75 26 11 64 6 95 71 71 99 46 69 98 6 7 5 42 99 84 38 19 62 16 64 16 65 53 56 37 53 91 15 30 2 51 30 25 15 86 46 42 93 2 80 78 35 29 62 86 23 99 50 82 6 27 82 22 57 72 12 27 10 14 52 85 33 10 5 79 100 82 5 28 97 98 96 45 6 14 89 95 82 97 1 98 38 81 60 26 6 68 86 77 93 62 40 13 74 76 43 31 9 33 20 72 5 11 73 47 100 16 23 56 52 85 60 19 66 23 59 44 56 4 49 62 23 96 82 89 99 57 41 44 70 43 4 30 43 89 96 29 43 40 39 61 1 41 87 58 75 84 78 36 47 14 62 58 2 100 95 29 10 1 54 47 96 38 45 35 2 15 4 26 49 42 54 86 69 79 33 22 67 50 22 75 23 64 76 34 52 74 30 30 13 87 27 58 45 61 66 66 37 11 30 6 76 69 28 84 24 80 70 48 95 23 6 80 20 94 36 53 71 5 84 67 7 37 32 31 63 7 36 11 42 31 94 30 2 51 56 50 34 99 100 98 92 92 51 44 15 46 41 39 77 3 93 24 27 69 60 42 4 29 98 7 36 36 7 53 50 6 47 93 67 62 9 30 40 37 85 41 74 31 16 80 37 94 96 33 75 44 95 29 78 63 65 97 7 90 6 59 28 71 44 63 56 91 9 68 73 18 43 57 38 64 41 89 31 91 77 35 82 54 15 62 7 64 92 73 75 25 18 60 23 38 36 62 99 83 5 54 1 87 34 88 40 62 87 77 91 30 97 85 12 98 81 100 97 78 12 71 24 45 92 29 9 15 50 98 94 6 24 2 26 11 77 8 50 81 19 13 61 30 16 80 97 10 52 36 35 63 61 4 38 53 72 2 73 33 87 89 3 28 20 15 55 47 61 71 34 9 88 64 37 65 9 63 33 28 73 21 56 4 82 63 1 26 53 73 83 42 91 37 24 53 62 94 52 71 85 73 59 41 39 90 31 93 53 22 80 57 83 67 14 37 96 53 29 76 59 68 60 12 41 17 16 92 88 62 25 79 86 56 20 7 39 11 67 95 35 89 99 9 8 83 76 50 58 20 2 19 3 6 2 32 97 86 70 55 54 79 66 51 80 74 19 41 81 80 21 3 16 9 84 63 68 72 26 19 5 58 68 38 57 35 26 9 89 7 16 24 72 25 51 90 50 26 15 12 12 40 17 36 96 37 24 37 5 98 96 3 8 39 72 23 12 96 32 77 86 28 77 15 12 13 68 18 93 57 72 92 74 64 96 68 50 41 80 81 52 53 66 19 16 10 24 96 89 92 33 14 3 70 77 86 73 50 25 41 8 86 50 92 28 18 12 89 26 98 38 20 99 58 100 67 17 31 70 54 96 23 59 58 90 67 36 60 5 5 42 56 4 45 9 26 64 68 71 12 49 99 56 78 94 28 79 3 6 42 84 25 31 82 24 63 75 66 78 64 16 37 23 74 96 14 41 72 58 2 91 1 15 44 88 19 77 39 61 99 19 60 8 13 53 24 86 1 91 88 23 27 44 74 21 65 9 14 70 51 13 6 42 76 28 82 52 74 7 32 59 50 66 79 17 17 76 29 91 6 46 39 55 85 42 64 70 15 38 80 52 19 90 26 42 73 98 68 3 13 72 70 98 69 32 6 14 14 69 10 29 21 55 62 63 6 29 92 19 97 56 25 12 68 69 6 25 83 80 70 88 38 49 50 20 24 40 90 73 80 63 74 78 81 74 17 61 65 74 65 5 7 7 35 100 57 93 12 6 51 96 5 29 26 38 58 83 67 23 97 33 27 11 85 93 75 65 41 98 4 8 51 6 21 10 16 73 52 26 78 49 73 14 64 5 20 98 48 57 6 88 98 26 7 20 40 14 75 66 45 59 69 17 83 88 75 58 80 16 87 57 63 70 69 43 83 64 70 78 31 100 64 45 82 24 36 27 62 61 8 53 25 22 9 45 88 62 20 50 49 79 67 98 88 97 38 63 10 54 58 49 48 83 91 18 8 27 37 55 35 6 56 79 50 69 44 93 18 92 29 95 61 48 31 52 100 42 42 61 6 87 6 76 66 89 49 33 88 67 3 85 7 1 85 35 3 11 61 89 57 21 94 72 32 37 25 80 11 65 19 2 74 75 36 78 49 24 27 91 6 44 62 25 19 82 80 54 76 59 54 12 50 43 95 6 84 28 27 65 25 10 2 21 31 63 8 68 22 71 61 35 95 22 14 70 38 33 81 44 10 23 17 71 68 33 96 93 23 4 23 84 43 69 39 95 71 57 88 46 4 30 10 82 98 66 27 41 64 86 48 84 52 78 15 61 44 83 48 68 96 41 63 69 60 14 98 8 32 34 75 7 75 49 19 55 67 2 36 81 54 58 25 95 98 32 84 39 41 65 25 8 64 19 71 43 64 38 70 19 66 71 36 32 11 60 21 52 71 59 18 59 95 11 79 40 52 61 92 49 79 35 49 64 66 82 41 50 40 43 97 25 99 2 12 28 85 76 98 77 85 66 85 34 4 27 95 20 35 1 82 58 46 98 33 97 82 6 68 31 35 87 73 1 41 37 79 60 92 27 22 43 97 46 49 55 55 100 45 15 74 24 63 62 87 8 40 2 76 67 51 14 21 4 47 59 40 78 49 83 28 90 19 2 8 26 94 30 86 76 53 4 10 2 19 10 9 66 88 56 85 73 91 82 67 80 18 89 94 95 69 10 14 25 96 81 97 28 7 22 33 13 31 47 62 3 30 42 48 93 79 38 28 96 28 80 4 17 12 49 9 91 28 8 33 77 52 81 52 43 14 19 86 89 42 10 90 95 84 25 1 19 6 69 66 8 69 40 24 65 99 75 72 33 11 74 29 36 4 77 10 90 37 66 3 95 40 30 6 47 5 80 27 98 77 77 5 50 60 21 12 23 8 12 48 87 44 56 75 76 88 50 14 27 9 35 67 70 27 21 95 57 84 87 78 24 24 97 32 88 15 35 83 7 87 40 44 88 87 97 70 19 27 55 88 37 29 50 91 4 92 3 88 58 18 81 3 47 27 6 8 27 51 55 13 45 36 18 80 32 74 30 46 39 31 57 36 40 99 61 14 68 14 4 93 47 75 90 45 61 57 55 43 68 24 45 38 84 43 10 20 83 35 74 24 55 62 60 39 20 57 11 4 18 23 59 67 86 87 67 8 48 3 33 20 78 58 24 27 55 11 22 77 5 44 46 91 39 69 7 52 61 48 98 57 54 69 8 73 73 62 20 25 67 41 39 39 88 77 1 71 99 69 99 70 91 39 14 91 7 88 51 48 43 48 42 66 96 21 68 87 67 7 80 23 62 99 78 8 32 48 79 27 9 97 60 32 35 64 41 1 26 75 83 74 83 41 61 82 13 35 31 66 27 35 72 90 57 93 30 48 73 68 15 42 93 59 67 6 62 1 11 23 82 70 4 22 59 43 88 86 51 87 92 77 15 94 75 42 60 90 14 91 7 9 93 64 11 11 52 48 100 64 1 88 74 36 85 96 84 77 86 20 43 73 56 96 86 68 62 28 95 48 59 79 61 11 84 29 36 88 92 26 6 7 43 21 83 54 11 13 71 32 4 54 6 8 2 79 100 13 93 83 34 37 64 34 65 14 84 42 54 84 35 85 34 52 28 44 3 24 34 100 26 72 21 20 34 61 83 95 9 16 55 12 33 22 6 23 78 33 18 62 38 77 4 80 74 92 37 72 15 70 49 61 63 25 35 44 72 88 90 55 37 43 88 9 61 14 32 23 24 81 94 81 41 32 28 47 80 54 41 13 15 64 60 49 20 27 34 16 85 29 61 52 4 20 97 33 4 83 99 88 64 15 15 52 66 1 46 99 74 40 37 8 12 65 45 55 39 81 3 | 91 102 4 | 11 127 5 | 124 137 6 | 104 110 7 | 19 81 8 | 137 145 9 | 42 61 10 | 56 113 11 | 43 119 12 | 94 151 13 | 123 131 14 | 46 102 15 | 149 151 16 | 66 93 17 | 93 120 18 | 53 69 19 | 144 147 20 | 97 121 21 | 111 113 22 | 6 7 23 | 73 78 24 | 28 104 25 | 147 148 26 | 149 150 27 | 57 80 28 | 138 148 29 | 144 147 30 | 18 52 31 | 100 121 32 | 113 132 33 | 144 148 34 | 47 77 35 | 29 48 36 | 2 91 37 | 120 145 38 | 58 63 39 | 29 127 40 | 35 124 41 | 36 71 42 | 33 124 43 | 138 146 44 | 115 117 45 | 72 86 46 | 37 61 47 | 149 150 48 | 68 121 49 | 28 94 50 | 83 92 51 | 77 115 52 | 74 136 53 | 92 149 54 | 106 135 55 | 93 136 56 | 120 150 57 | 99 111 58 | 70 75 59 | 19 121 60 | 62 110 61 | 52 125 62 | 89 123 63 | 29 90 64 | 1 121 65 | 137 140 66 | 43 137 67 | 130 145 68 | 56 147 69 | 92 95 70 | 4 19 71 | 75 86 72 | 11 41 73 | 130 145 74 | 122 141 75 | 20 119 76 | 29 135 77 | 21 55 78 | 139 146 79 | 144 148 80 | 130 132 81 | 52 55 82 | 2 92 83 | 74 96 84 | 58 117 85 | 92 96 86 | 31 103 87 | 59 60 88 | 34 69 89 | 83 143 90 | 1 124 91 | 52 87 92 | 36 68 93 | 18 103 94 | 31 54 95 | 41 129 96 | 72 138 97 | 32 145 98 | 121 151 99 | 146 150 100 | 38 147 101 | 102 149 102 | 116 143 103 | 19 126 104 | 63 85 105 | 85 128 106 | 97 142 107 | 83 102 108 | 47 88 109 | 146 150 110 | 66 116 111 | 58 106 112 | 11 98 113 | 138 143 114 | 92 101 115 | 45 53 116 | 88 113 117 | 113 115 118 | 59 87 119 | 72 144 120 | 25 150 121 | 119 145 122 | 55 142 123 | 138 149 124 | 64 107 125 | 10 36 126 | 72 134 127 | 138 143 128 | 10 69 129 | 58 83 130 | 72 119 131 | 107 120 132 | 80 99 133 | 15 45 134 | 71 75 135 | 58 131 136 | 74 90 137 | 28 135 138 | 13 122 139 | 37 78 140 | 115 127 141 | 125 128 142 | 60 143 143 | 50 94 144 | 19 117 145 | 8 65 146 | 87 99 147 | 44 133 148 | 88 97 149 | 45 107 150 | 110 130 151 | 122 123 152 | 65 146 153 | 44 115 154 | 145 146 155 | 133 146 156 | 116 123 157 | 45 128 158 | 138 148 159 | 53 105 160 | 69 97 161 | 86 142 162 | 141 150 163 | 143 145 164 | 116 134 165 | 28 86 166 | 138 146 167 | 53 104 168 | 123 134 169 | 24 72 170 | 107 147 171 | 59 75 172 | 145 148 173 | 67 70 174 | 136 137 175 | 96 119 176 | 118 138 177 | 4 53 178 | 38 92 179 | 146 149 180 | 84 105 181 | 95 145 182 | 13 79 183 | 11 40 184 | 96 127 185 | 13 83 186 | 69 100 187 | 20 91 188 | 46 62 189 | 75 97 190 | 77 146 191 | 19 45 192 | 98 144 193 | 29 143 194 | 50 65 195 | 136 143 196 | 129 148 197 | 87 130 198 | 143 151 199 | 50 81 200 | 24 108 201 | 48 85 202 | 133 147 203 | 61 89 204 | 16 34 205 | 30 85 206 | 16 85 207 | 71 123 208 | 18 33 209 | 135 147 210 | 113 136 211 | 90 124 212 | 83 133 213 | 84 151 214 | 68 92 215 | 112 139 216 | 106 136 217 | 26 62 218 | 137 144 219 | 36 122 220 | 43 118 221 | 118 121 222 | 97 103 223 | 137 147 224 | 35 40 225 | 71 127 226 | 128 137 227 | 135 140 228 | 133 140 229 | 91 119 230 | 43 49 231 | 47 103 232 | 75 89 233 | 14 75 234 | 16 83 235 | 4 34 236 | 89 142 237 | 110 144 238 | 71 92 239 | 32 80 240 | 75 113 241 | 74 150 242 | 11 137 243 | 114 128 244 | 28 51 245 | 150 151 246 | 106 147 247 | 62 108 248 | 115 134 249 | 21 121 250 | 66 119 251 | 81 142 252 | 42 108 253 | 141 151 254 | 76 130 255 | 107 138 256 | 113 146 257 | 59 108 258 | 81 118 259 | 88 149 260 | 99 134 261 | 66 104 262 | 44 72 263 | 23 146 264 | 91 97 265 | 49 125 266 | 123 147 267 | 113 129 268 | 10 39 269 | 149 151 270 | 50 68 271 | 71 94 272 | 137 143 273 | 129 139 274 | 35 62 275 | 94 128 276 | 143 145 277 | 33 41 278 | 44 79 279 | 83 144 280 | 99 138 281 | 53 117 282 | 1 18 283 | 11 17 284 | 82 86 285 | 79 105 286 | 127 139 287 | 45 149 288 | 51 72 289 | 11 118 290 | 21 99 291 | 60 97 292 | 40 71 293 | 48 61 294 | 104 134 295 | 138 143 296 | 69 83 297 | 66 86 298 | 146 151 299 | 34 140 300 | 59 108 301 | 103 108 302 | 91 141 303 | 82 147 304 | 111 117 305 | 56 106 306 | 101 117 307 | 119 143 308 | 19 113 309 | 104 136 310 | 94 99 311 | 60 117 312 | 79 145 313 | 141 150 314 | 54 81 315 | 42 81 316 | 97 101 317 | 116 125 318 | 58 100 319 | 101 104 320 | 135 147 321 | 8 94 322 | 105 116 323 | 147 148 324 | 89 137 325 | 65 78 326 | 63 109 327 | 58 115 328 | 123 147 329 | 41 71 330 | 71 102 331 | 75 90 332 | 115 150 333 | 20 132 334 | 12 89 335 | 22 73 336 | 7 115 337 | 14 31 338 | 77 105 339 | 78 131 340 | 96 135 341 | 102 125 342 | 92 109 343 | 117 146 344 | 111 123 345 | 53 57 346 | 139 140 347 | 94 116 348 | 45 99 349 | 106 142 350 | 133 145 351 | 10 141 352 | 31 69 353 | 66 106 354 | 76 136 355 | 33 103 356 | 31 143 357 | 13 73 358 | 11 88 359 | 95 112 360 | 101 115 361 | 38 94 362 | 136 148 363 | 100 108 364 | 21 142 365 | 7 70 366 | 19 107 367 | 84 113 368 | 87 145 369 | 43 101 370 | 30 87 371 | 127 129 372 | 34 76 373 | 83 127 374 | 101 135 375 | 140 148 376 | 68 109 377 | 28 136 378 | 137 138 379 | 3 70 380 | 93 130 381 | 31 57 382 | 147 149 383 | 71 111 384 | 94 109 385 | 35 41 386 | 145 146 387 | 116 144 388 | 147 149 389 | 140 147 390 | 22 137 391 | 143 144 392 | 118 129 393 | 34 146 394 | 82 93 395 | 118 140 396 | 9 138 397 | 57 140 398 | 95 128 399 | 74 113 400 | 62 82 401 | 86 109 402 | 126 146 403 | 74 135 404 | 125 135 405 | 19 91 406 | 120 150 407 | 50 118 408 | 72 124 409 | 36 121 410 | 111 144 411 | 4 109 412 | 47 102 413 | 41 111 414 | 9 72 415 | 18 123 416 | 56 144 417 | 96 100 418 | 149 150 419 | 8 119 420 | 72 136 421 | 43 108 422 | 119 129 423 | 66 72 424 | 103 139 425 | 23 25 426 | 78 108 427 | 6 79 428 | 5 48 429 | 112 140 430 | 145 149 431 | 36 125 432 | 139 150 433 | 46 141 434 | 120 147 435 | 8 13 436 | 25 68 437 | 131 147 438 | 47 85 439 | 86 141 440 | 147 150 441 | 45 52 442 | 105 132 443 | 81 150 444 | 144 146 445 | 24 87 446 | 113 147 447 | 2 99 448 | 137 147 449 | 84 96 450 | 71 96 451 | 109 141 452 | 86 137 453 | 36 113 454 | 60 71 455 | 45 143 456 | 6 125 457 | 37 109 458 | 96 143 459 | 59 95 460 | 106 134 461 | 84 122 462 | 137 143 463 | 145 151 464 | 96 147 465 | 150 151 466 | 41 128 467 | 35 56 468 | 110 143 469 | 8 82 470 | 114 117 471 | 18 74 472 | 131 141 473 | 94 124 474 | 19 24 475 | 6 50 476 | 52 93 477 | 145 146 478 | 123 137 479 | 55 137 480 | 139 142 481 | 30 63 482 | 114 151 483 | 18 149 484 | 125 131 485 | 100 120 486 | 33 69 487 | 106 114 488 | 71 105 489 | 18 24 490 | 75 131 491 | 85 150 492 | 141 147 493 | 148 150 494 | 74 110 495 | 75 151 496 | 59 132 497 | 141 148 498 | 24 41 499 | 96 99 500 | 119 125 501 | 6 52 502 | 138 151 503 | -------------------------------------------------------------------------------- /week4/2-Birthday-Ranges/maycamp/bday-ranges.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week4/2-Birthday-Ranges/maycamp/bday-ranges.zip -------------------------------------------------------------------------------- /week4/2-Birthday-Ranges/maycamp/description.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week4/2-Birthday-Ranges/maycamp/description.pdf -------------------------------------------------------------------------------- /week4/3-String-Normalizer/README.md: -------------------------------------------------------------------------------- 1 | # A String Normalizer 2 | 3 | In a package `com.hackbulgaria.programming51.week4` do the following: 4 | 5 | You are given the task to normalize all the strings in a file. 6 | A normalized string is a string where: 7 | 8 | * Every word begins with a capital letter 9 | * All the other letters in the word are lowercase 10 | * There is a maximum of 1 whitespace (interval) between two words 11 | * There are no spaces in the beginning or in the end of the string 12 | 13 | ## The Task: 14 | 15 | In a `class StringNormalizer` create a static method `public static String normalizeString(String s)` which recieves a string `s` and returns a normalized version of `s` 16 | 17 | ## `class StringNormalizer` shlould look like: 18 | 19 | ```java 20 | package week4; 21 | 22 | import java.util.Scanner; 23 | 24 | public class StringNormalizer { 25 | 26 | public static String normalizeString(String str) { 27 | // TODO: implementation here 28 | } 29 | 30 | } 31 | ``` 32 | 33 | ## Examples: 34 | 35 | **Here is a boilerplate class ready to take console input:** 36 | 37 | ```java 38 | package week4; 39 | 40 | import java.util.Scanner; 41 | 42 | public class StringNormalizer { 43 | 44 | public static String normalizeString(String str) { 45 | // TODO: implementation here 46 | } 47 | 48 | public static void main(String[] args) { 49 | 50 | Scanner scanner = new Scanner(System.in); 51 | 52 | String s = scanner.nextLine(); 53 | 54 | System.out.println(normalizeString(s)); 55 | } 56 | 57 | } 58 | ``` 59 | 60 | 61 | And now, for input: 62 | 63 | ``` 64 | wHaT iS UP? 65 | ``` 66 | 67 | We should get output: 68 | 69 | ``` 70 | What Is Up? 71 | ``` 72 | 73 | --- 74 | 75 | And for input: 76 | 77 | ``` 78 | HACK bULGARIA iS a Cool PLACE for PROGRAMMING! 79 | ``` 80 | 81 | We should get output: 82 | 83 | ``` 84 | Hack Bulgaria Is A Cool Place For Programming! 85 | ``` -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/description.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week4/3-String-Normalizer/maycamp/description.pdf -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/generate_tests.py: -------------------------------------------------------------------------------- 1 | from random import choice, randint 2 | 3 | def corrupt(word): 4 | chars = [] 5 | 6 | for ch in word: 7 | if randint(0, 5) < 4: 8 | ch = ch.upper() 9 | 10 | chars.append(ch) 11 | 12 | word = "".join(chars) 13 | 14 | if randint(0, 1) == 0: 15 | word = " " * randint(0, 10) + word 16 | else: 17 | word = word + " " * randint(0, 10) 18 | 19 | return word 20 | 21 | def test_sentence(dictionary, number_of_words=10): 22 | count = 0 23 | sentence = set() 24 | 25 | while count < number_of_words: 26 | next_word = choice(dictionary) 27 | 28 | while next_word in sentence: 29 | next_word = choice(dictionary) 30 | 31 | next_word = corrupt(next_word) 32 | 33 | sentence.add(next_word) 34 | count += 1 35 | 36 | return (" " * randint(1, 10)).join(sentence) + " " * randint(1, 10) 37 | 38 | 39 | def normalize(sentence): 40 | return " ".join([word.strip().lower().capitalize() for word in sentence.split(" ") if word.strip() != ""]) 41 | 42 | 43 | dict_path = "/usr/share/dict/american-english" 44 | words = [] 45 | base_name = "str" 46 | 47 | with open(dict_path, "r") as f: 48 | words = f.read().split("\n") 49 | 50 | 51 | for index in range(0, 10): 52 | current_input = test_sentence(words) 53 | output = normalize(current_input) 54 | 55 | with open("{}00{}.in".format(base_name, index), "w") as f: 56 | f.write(current_input) 57 | 58 | with open("{}00{}.ans".format(base_name, index), "w") as f: 59 | f.write(output) 60 | f.write("\n") 61 | 62 | -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str001.ans: -------------------------------------------------------------------------------- 1 | Aiding Restfullest Cheetah's Palate Boyhood Inseminating Remakes Introvert's Whale Cameroons 2 | -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str001.in: -------------------------------------------------------------------------------- 1 | aIDInG rEStFULlEsT cHeetAH'S pAlATE BOYhooD INsEmInaTinG rEMAkeS InTROVeRT'S WhALe CAMErOOnS -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str002.ans: -------------------------------------------------------------------------------- 1 | Reclamation's Throbbed Says Action's Signalized Derided Attitudinizing Convert's Russ Flattening 2 | -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str002.in: -------------------------------------------------------------------------------- 1 | REcLAMAtION's THRoBBED SAyS ActION'S SIGnaliZeD derIdED ATtITUDINizIng COnveRt'S RUSS FlATTeNIng -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str003.ans: -------------------------------------------------------------------------------- 1 | Comely Solo's Depart Krystal Aquiculture Bane's Nasturtiums Pothook Sonny Replica 2 | -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str003.in: -------------------------------------------------------------------------------- 1 | COMeLY SoLO'S dEPART KRYStAl aqUIcUlTURE BAnE's NAStuRtIUMS PothoOK SOnNy REPLICA -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str004.ans: -------------------------------------------------------------------------------- 1 | Rattle's Hockshops Substructure's Lots Jivaro Zimbabwe Graven Formal's Liveliness's Sarajevo 2 | -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str004.in: -------------------------------------------------------------------------------- 1 | RATTLe'S HoCkshOPS SuBstRuCtURE's LOTS JIVARO ZImbABWE GRAvEN FoRmAl'S LIVeLInESs's SarAJeVO -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str005.ans: -------------------------------------------------------------------------------- 1 | Imprisonments Mutate Sparingly Smithsonian's Blondes Deduction's Stiffing Ambushing Apostle Shuttlecock's 2 | -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str005.in: -------------------------------------------------------------------------------- 1 | IMpRISONMENts mUtaTe SParInGLY SMITHsOniAN'S BlOndeS DedUCTIoN'S sTiffiNG aMBushING apOStLE sHUTTLECOck'S -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str006.ans: -------------------------------------------------------------------------------- 1 | Leads Embraces Pansy Circularity Rebroadcasts Harms Derby Cordiality's Elliptically Insecurely 2 | -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str006.in: -------------------------------------------------------------------------------- 1 | LEads EMBRACES PANsY CIrcULaRITY REBrOAdcAsTS HArmS DERBY cORDialiTY'S ELlIPTICAlLy INSECURElY -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str007.ans: -------------------------------------------------------------------------------- 1 | Undulations Node's Capriciously Facing's Treasurer's Apiary Chuck's Maldive's Miniscules Waste 2 | -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str007.in: -------------------------------------------------------------------------------- 1 | UNdulATIOns NODe'S CAPRicIOusly FaCInG'S trEASuRER'S aPiARY CHUCk'S MALDiVE's MINIsCuLES wASTe -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str008.ans: -------------------------------------------------------------------------------- 1 | Carr's Stairwell's Tabatha Bosch Obscurely Teasdale's Flotations Coif Deposes Correspondents 2 | -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str008.in: -------------------------------------------------------------------------------- 1 | CarR'S sTAIRWElL'S TABATHa BOscH ObsCURELY TEASDalE'S FloTATionS COIF DEPOSEs CorrESPonDeNTs -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str009.ans: -------------------------------------------------------------------------------- 1 | Freighter's Saucers Isidro Languid Bookshop Crossfire's Amman Rossetti Sent Schwarzenegger's 2 | -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/str009.in: -------------------------------------------------------------------------------- 1 | FreIGHTeR's SaUCErS IsiDrO lANGUId bOoKShOp CROSsFiRE's AMMan ROSsETtI SENT SCHWARZEnEgGER's -------------------------------------------------------------------------------- /week4/3-String-Normalizer/maycamp/string-normalizer.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week4/3-String-Normalizer/maycamp/string-normalizer.zip -------------------------------------------------------------------------------- /week4/4-Vector-of-Vectors/README.md: -------------------------------------------------------------------------------- 1 | # A Vector of Vectors 2 | 3 | In a package `com.hackbulgaria.programming51.week4` do the following: 4 | 5 | You are given the following data structure: `Vector< Vector >` 6 | 7 | ## The task: 8 | 9 | In a `class VectorOfVectors` create a static method `public static Vector sortAndUnique(Vector< Vector >)` that recieves a vector of vectors of integers, sorts them and filters only the unique numbers. Return the result in a Vector of integers. 10 | 11 | ## `class VectorOfVectors` should look like: 12 | 13 | ```java 14 | package week4; 15 | 16 | public class VectorOfVectors { 17 | 18 | public static Vector sortAndUnique(Vector> v){ 19 | 20 | return null; 21 | } 22 | 23 | } 24 | ``` 25 | 26 | ## Examples: 27 | 28 | **Here is a boilerplate class ready to take console input:** 29 | 30 | ```java 31 | package week4; 32 | 33 | import java.util.Scanner; 34 | 35 | public class VectorOfVectors { 36 | 37 | public static Vector sortAndUnique(Vector> v) { 38 | // TODO: add implementation here 39 | return null; 40 | } 41 | 42 | public static void main(String[] args) { 43 | 44 | Vector> vi; 45 | Scanner scanner = new Scanner(System.in); 46 | 47 | int n = scanner.nextInt(); 48 | for (int i = 0; i < n; i++) { 49 | Vector v = new Vector(); 50 | int num = scanner.nextInt(); 51 | for (int k = 0; k < num; k++) { 52 | v.add(scanner.nextInt()); 53 | } 54 | vi.add(v); 55 | } 56 | System.out.println(sortAndUnique(vi)); 57 | } 58 | 59 | } 60 | ``` 61 | 62 | And now, for input: 63 | 64 | ``` 65 | 3 66 | 5 5 10 -20 4 50 67 | 3 1 55 28 68 | 4 20 10 4 50 69 | ``` 70 | 71 | We should get output: 72 | ``` 73 | -20 1 4 5 10 20 28 50 55 74 | ``` 75 | 76 | --- 77 | 78 | And for input: 79 | 80 | ``` 81 | 5 82 | 3 4 50 120 83 | 3 1 55 28 84 | 4 20 10 4 -19 85 | 2 -30 10 86 | 5 3 5 2 7 4 87 | ``` 88 | 89 | We should get output: 90 | ``` 91 | -30 -19 1 2 3 4 5 7 10 20 28 50 55 120 92 | ``` 93 | -------------------------------------------------------------------------------- /week4/5-Books-of-Lili/README.md: -------------------------------------------------------------------------------- 1 | # Books of Lili 2 | 3 | There is a girl named Lili. She loves books. She has many books. 4 | 5 | But it is total chaos - there are many copies of different titles and the books cannot be searched easily. 6 | 7 | So you have to help Lili. She wants to sort her books alphabetically and also, know for each title, how man copies she have. 8 | 9 | Implement a class called `LilisLibrary` where you have the following static method: 10 | 11 | ```java 12 | public class LilisLibrary { 13 | 14 | public static Vector> orderBooks(Vector books) { 15 | // implementation 16 | } 17 | } 18 | ``` 19 | 20 | The result should be a `Vector` of `Pairs` where each `Pair` has: 21 | 22 | * The book title 23 | * Count of how many books with that title there is 24 | 25 | **Also, sort the result by the titles of the book, alphabetically, from a -> z** 26 | 27 | ## Generic Pair class 28 | 29 | ```java 30 | public class Pair { 31 | 32 | public F first; 33 | public S second; 34 | } 35 | ``` 36 | 37 | ## Examples 38 | 39 | Have this boilerplate: 40 | 41 | ```java 42 | package com.hackbulgaria.programming51.week4; 43 | 44 | import java.util.Scanner; 45 | import java.util.Vector; 46 | 47 | public class LilisLibrary { 48 | public static Vector> orderBooks(Vector books) { 49 | return new Vector>(); 50 | } 51 | 52 | public static void main(String[] args) { 53 | Scanner scanner = new Scanner(System.in); 54 | 55 | Vector books = new Vector(); 56 | int booksCount = Integer.parseInt(scanner.nextLine()); 57 | 58 | for (int i = 0; i < booksCount; i++) { 59 | books.add(scanner.nextLine()); 60 | } 61 | 62 | Vector> result = orderBooks(books); 63 | 64 | for(Pair titleCount: result) { 65 | System.out.println(titleCount.first + " : " + titleCount.second); 66 | } 67 | } 68 | } 69 | ``` 70 | 71 | For input: 72 | 73 | ``` 74 | 6 75 | Harry Potter 76 | Harry Potter 77 | The book of books 78 | Harry Potter 79 | Night Watch 80 | Thud! 81 | ``` 82 | 83 | Give the following output: 84 | 85 | ``` 86 | Harry Potter : 3 87 | Night Watch : 1 88 | The book of books : 1 89 | Thud! : 1 90 | ``` 91 | 92 | --- 93 | 94 | For input: 95 | 96 | ``` 97 | 10 98 | Z 99 | C 100 | A 101 | A 102 | D 103 | F 104 | G 105 | H 106 | I 107 | C 108 | ``` 109 | 110 | Give the following output: 111 | 112 | ``` 113 | A : 2 114 | C : 2 115 | D : 1 116 | F : 1 117 | G : 1 118 | H : 1 119 | I : 1 120 | Z : 1 121 | ``` 122 | -------------------------------------------------------------------------------- /week4/6-Missing-Students/README.md: -------------------------------------------------------------------------------- 1 | # Missing Students 2 | 3 | You are implementing a classroom software. You have to datasets: 4 | 5 | * All students in your classroom. 6 | * A list of students, for each day, that were present during the class. 7 | 8 | You have to write some code to determine which students were mising for each day during the class. 9 | 10 | For example, if you have the students: 11 | 12 | ``` 13 | Maria, Kamen, Ivo, Tanya, Rado 14 | ``` 15 | 16 | And the presence: 17 | 18 | ``` 19 | Day1 - Kamen, Ivo 20 | Day2 - Maria, Rado 21 | ``` 22 | 23 | Then the missing students for each day are: 24 | 25 | ``` 26 | Missing students for Day1 - Maria, Tanya, Rado 27 | Missing students for Day2 - Kamen, Ivo, Tanya 28 | ``` 29 | 30 | In a class, called `MissingStudents`, implement the following static method: 31 | 32 | ```java 33 | public class MissingStudents { 34 | 35 | public static Vector> missingStudents(Vector students, Vector> presence) { 36 | // implementation 37 | } 38 | } 39 | 40 | ``` 41 | 42 | * The first argument - `students` consist of all students (with no repetitions) taking a given class. 43 | * The second argument - `presence` consists of vector of students that have been present for the given day (lets say the index of the vector is the day that we are talking about) 44 | 45 | The result should be a vector of vectors, where each element is a list of students, missing for that day. 46 | 47 | ## Boilerplate 48 | 49 | Here is the code, ready for taking input: 50 | 51 | ```java 52 | package com.hackbulgaria.programming51.week4; 53 | 54 | import java.util.Scanner; 55 | import java.util.Vector; 56 | 57 | public class MissingStudents { 58 | 59 | public static Vector> missingStudents( 60 | Vector students, Vector> presence) { 61 | 62 | return new Vector>(); 63 | } 64 | 65 | public static void main(String[] args) { 66 | Scanner scanner = new Scanner(System.in); 67 | Vector students = new Vector<>(); 68 | Vector> presence = new Vector<>(); 69 | 70 | int studentsCount = Integer.parseInt(scanner.nextLine()); 71 | 72 | for (int i = 0; i < studentsCount; i++) { 73 | String student = scanner.nextLine(); 74 | students.add(student); 75 | } 76 | 77 | int days = Integer.parseInt(scanner.nextLine()); 78 | 79 | for (int i = 0; i < days; i++) { 80 | int studentsForThatDay = Integer.parseInt(scanner.nextLine()); 81 | Vector presenceForDay = new Vector<>(); 82 | for (int j = 0; j < studentsForThatDay; j++) { 83 | presenceForDay.add(scanner.nextLine()); 84 | } 85 | 86 | presence.add(presenceForDay); 87 | } 88 | 89 | Vector> missing = missingStudents(students, presence); 90 | 91 | System.out.println(missing); 92 | } 93 | } 94 | ``` 95 | 96 | 97 | ## Examples 98 | 99 | For the input: 100 | 101 | ``` 102 | 5 103 | Maria 104 | Kamen 105 | Ivo 106 | Tanya 107 | Rado 108 | 2 109 | 2 110 | Kamen 111 | Ivo 112 | 2 113 | Maria 114 | Rado 115 | ``` 116 | 117 | Give the following output: 118 | 119 | ``` 120 | 121 | [[Maria, Rado, Tanya], [Ivo, Kamen, Tanya]] 122 | ``` 123 | 124 | --- 125 | 126 | For the following input: 127 | 128 | ``` 129 | 5 130 | Ivo 131 | Rado 132 | Maria 133 | Aneta 134 | Tanya 135 | 4 136 | 4 137 | Ivo 138 | Maria 139 | Aneta 140 | Tanya 141 | 4 142 | Ivo 143 | Maria 144 | Aneta 145 | Tanya 146 | 3 147 | Ivo 148 | Aneta 149 | Tanya 150 | 1 151 | Ivo 152 | ``` 153 | 154 | **Give the following output, where each day, the missing students are sorted a->z** 155 | 156 | ``` 157 | [[Rado], [Rado], [Rado, Maria], [Rado, Maria, Aneta, Tanya]] 158 | ``` 159 | 160 | ### Explanation of the input 161 | 162 | With comments: (do not paste into Java console, not a valid input.) 163 | 164 | ``` 165 | 5 -> total number of students 166 | Ivo 167 | Rado 168 | Maria 169 | Aneta 170 | Tanya 171 | 4 -> total number of days 172 | 4 -> How many students during first day 173 | Ivo 174 | Maria 175 | Aneta 176 | Tanya 177 | 4 -> How many students during second day 178 | Ivo 179 | Maria 180 | Aneta 181 | Tanya 182 | 3 -> How many students during third day 183 | Ivo 184 | Aneta 185 | Tanya 186 | 1 -> How many students during fourth day 187 | Ivo 188 | ``` 189 | -------------------------------------------------------------------------------- /week4/6-Missing-Students/maycamp/description.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week4/6-Missing-Students/maycamp/description.pdf -------------------------------------------------------------------------------- /week4/6-Missing-Students/maycamp/m.ans001: -------------------------------------------------------------------------------- 1 | [[Rado], [Rado], [Maria, Rado], [Aneta, Maria, Rado, Tanya]] 2 | -------------------------------------------------------------------------------- /week4/6-Missing-Students/maycamp/m.ans002: -------------------------------------------------------------------------------- 1 | [[Maria, Rado, Tanya], [Ivo, Kamen, Tanya]] 2 | -------------------------------------------------------------------------------- /week4/6-Missing-Students/maycamp/m.in001: -------------------------------------------------------------------------------- 1 | 5 2 | Ivo 3 | Rado 4 | Maria 5 | Aneta 6 | Tanya 7 | 4 8 | 4 9 | Ivo 10 | Maria 11 | Aneta 12 | Tanya 13 | 4 14 | Ivo 15 | Maria 16 | Aneta 17 | Tanya 18 | 3 19 | Ivo 20 | Aneta 21 | Tanya 22 | 1 23 | Ivo 24 | -------------------------------------------------------------------------------- /week4/6-Missing-Students/maycamp/m.in002: -------------------------------------------------------------------------------- 1 | 5 2 | Maria 3 | Kamen 4 | Ivo 5 | Tanya 6 | Rado 7 | 2 8 | 2 9 | Kamen 10 | Ivo 11 | 2 12 | Maria 13 | Rado 14 | -------------------------------------------------------------------------------- /week4/6-Missing-Students/maycamp/missing-students.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week4/6-Missing-Students/maycamp/missing-students.zip -------------------------------------------------------------------------------- /week4/7-Max-Span/README.md: -------------------------------------------------------------------------------- 1 | # Max Span 2 | 3 | Consider the leftmost and righmost appearances of some value in an array: 4 | 5 | * **We'll say that the "span" is the number of elements between the two inclusive.** 6 | * A single value has a span of 1. 7 | 8 | For example, if we have the array `1, 2, 1, 1, 3`, the max span is between 1 and 1 with value `4`: `-->1, 2, 1, 1<--, 3` 9 | 10 | In a class called `MaxSpan`, implement a static method that **returns the largest span found in the given array.** 11 | 12 | ## Boilerplate 13 | 14 | ```java 15 | import java.util.Scanner; 16 | 17 | public class MaxSpan { 18 | 19 | public static int maxSpan(int[] numbers) { 20 | return 0; 21 | } 22 | 23 | public static void main(String[] args) { 24 | Scanner scanner = new Scanner(System.in); 25 | 26 | int n = scanner.nextInt(); 27 | int[] numbers = new int[n]; 28 | 29 | for(int i = 0; i < n; i++) { 30 | numbers[i] = scanner.nextInt(); 31 | } 32 | 33 | System.out.println(maxSpan(numbers)); 34 | } 35 | 36 | } 37 | ``` 38 | 39 | ## Examples 40 | 41 | Input: 42 | 43 | ``` 44 | 5 45 | 1 2 1 1 3 46 | ``` 47 | 48 | Output: 49 | 50 | ``` 51 | 4 52 | ``` 53 | 54 | --- 55 | 56 | Input: 57 | 58 | ``` 59 | 7 60 | 1 4 2 1 4 1 4 61 | ``` 62 | 63 | Output: 64 | 65 | ``` 66 | 6 67 | ``` 68 | 69 | --- 70 | 71 | Input: 72 | 73 | ``` 74 | 7 75 | 1 4 2 1 4 4 4 76 | ``` 77 | 78 | Output: 79 | 80 | ``` 81 | 6 82 | ``` 83 | 84 | --- 85 | 86 | Input: 87 | 88 | ``` 89 | 10 90 | 1 1 1 1 1 1 1 1 1 1 91 | ``` 92 | 93 | Output: 94 | 95 | ``` 96 | 10 97 | ``` 98 | -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/description.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week4/7-Max-Span/maycamp/description.pdf -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max-span.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week4/7-Max-Span/maycamp/max-span.zip -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max001.ans: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max001.in: -------------------------------------------------------------------------------- 1 | 5 2 | 1 2 1 1 3 3 | -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max002.ans: -------------------------------------------------------------------------------- 1 | 6 2 | -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max002.in: -------------------------------------------------------------------------------- 1 | 7 2 | 1 4 2 1 4 1 4 3 | -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max003.ans: -------------------------------------------------------------------------------- 1 | 6 2 | -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max003.in: -------------------------------------------------------------------------------- 1 | 7 2 | 1 4 2 1 4 4 4 3 | -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max004.ans: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max004.in: -------------------------------------------------------------------------------- 1 | 10 2 | 1 1 1 1 1 1 1 1 1 1 3 | -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max005.ans: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max005.in: -------------------------------------------------------------------------------- 1 | 7 2 | 1 2 3 4 5 6 7 3 | -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max006.ans: -------------------------------------------------------------------------------- 1 | 78 2 | -------------------------------------------------------------------------------- /week4/7-Max-Span/maycamp/max006.in: -------------------------------------------------------------------------------- 1 | 100 2 | 30 5 75 30 1 9 35 54 26 17 60 61 89 70 35 56 74 39 29 89 89 90 17 80 6 57 72 79 33 59 40 5 51 44 94 62 61 47 12 49 24 27 38 83 74 49 75 100 39 75 63 62 34 66 65 66 41 28 95 99 73 52 32 31 88 4 76 35 33 48 6 93 15 51 68 96 38 78 33 92 12 91 36 34 36 81 76 80 61 67 24 0 33 27 46 36 36 85 6 37 3 | -------------------------------------------------------------------------------- /week4/8-Anagrams/README.md: -------------------------------------------------------------------------------- 1 | # Are two words anagrams? 2 | 3 | In a class `Anagrams`, implement a static method `boolen areAnagrams(String a, String b)`. 4 | 5 | For anagrams, check here - https://en.wikipedia.org/wiki/Anagram 6 | 7 | For example, `listen` and `silent` are anagrams. 8 | 9 | The program should read two words from the standard input and output: 10 | 11 | * `ANAGRAMS` if the words are anagrams of each other 12 | * `NOT ANAGRAMS` if the two words are not anagrams of each other 13 | 14 | **Consider lowering the case of the two words since the case does not matter. `SILENT` and `listen` are also anagrams.** 15 | 16 | ## Boilerplate 17 | 18 | ```java 19 | import java.util.Scanner; 20 | 21 | public class Anagrams { 22 | 23 | public static boolean areAnagrams(String a, String b) { 24 | return false; 25 | } 26 | 27 | public static void main(String[] args) { 28 | Scanner scanner = new Scanner(System.in); 29 | 30 | String a = scanner.next(); 31 | String b = scanner.next(); 32 | 33 | if (areAnagrams(a, b)) { 34 | System.out.println("ANAGRAMS"); 35 | } else { 36 | System.out.println("NOT ANAGRAMS"); 37 | } 38 | } 39 | } 40 | ``` 41 | 42 | ## Examples 43 | 44 | Input: 45 | 46 | ``` 47 | silent listen 48 | ``` 49 | 50 | Output: 51 | 52 | ``` 53 | ANAGRAMS 54 | ``` 55 | 56 | --- 57 | 58 | Input: 59 | 60 | ``` 61 | TOP_CODER COTO_PRODE 62 | ``` 63 | 64 | Output: 65 | 66 | ``` 67 | NOT ANAGRAMS 68 | ``` 69 | 70 | --- 71 | 72 | Input: 73 | 74 | ``` 75 | kilata cvetelina_yaneva 76 | ``` 77 | 78 | Output: 79 | 80 | ``` 81 | NOT ANAGRAMS 82 | ``` 83 | 84 | Also, should not make songs together. 85 | 86 | --- 87 | 88 | Input: 89 | 90 | ``` 91 | BRADE BEARD 92 | ``` 93 | 94 | Output: 95 | 96 | ``` 97 | ANAGRAMS 98 | ``` 99 | -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram000.ans: -------------------------------------------------------------------------------- 1 | NOT ANAGRAMS 2 | -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram000.in: -------------------------------------------------------------------------------- 1 | rlntuakilwldiycngqxk lomjbxmzldjrusoyvmhe -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram001.ans: -------------------------------------------------------------------------------- 1 | ANAGRAMS 2 | -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram001.in: -------------------------------------------------------------------------------- 1 | bghuiadwipbclxtlhurq uquiwhdihabcbgltxlpr -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram002.ans: -------------------------------------------------------------------------------- 1 | ANAGRAMS 2 | -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram002.in: -------------------------------------------------------------------------------- 1 | hgusvmmjrbdhjxinhvtw mijgbhhnwdvvxhsrtumj -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram003.ans: -------------------------------------------------------------------------------- 1 | ANAGRAMS 2 | -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram003.in: -------------------------------------------------------------------------------- 1 | tiamfhrjrlbuvwobqmtt morbljirthtwmvtaqbfu -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram004.ans: -------------------------------------------------------------------------------- 1 | ANAGRAMS 2 | -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram004.in: -------------------------------------------------------------------------------- 1 | uyylccqcernsuxekgrzo syrkzuqocyegcenruxcl -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram005.ans: -------------------------------------------------------------------------------- 1 | NOT ANAGRAMS 2 | -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram005.in: -------------------------------------------------------------------------------- 1 | fmcgdsrsgjtthgofhudh nedhpgtuhieuihstvgki -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram006.ans: -------------------------------------------------------------------------------- 1 | NOT ANAGRAMS 2 | -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram006.in: -------------------------------------------------------------------------------- 1 | mgpgjwnivnnleetrfpat jxuqfhogfoouqmshbnkw -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram007.ans: -------------------------------------------------------------------------------- 1 | ANAGRAMS 2 | -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram007.in: -------------------------------------------------------------------------------- 1 | hjckcczrngkpxopfttdf cpkjhffrcgztopctndkx -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram008.ans: -------------------------------------------------------------------------------- 1 | NOT ANAGRAMS 2 | -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagram008.in: -------------------------------------------------------------------------------- 1 | hywptcysbnafewazymqz fzb{{zuxzotqbdicxngr -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/anagrams.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week4/8-Anagrams/maycamp/anagrams.zip -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/description.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week4/8-Anagrams/maycamp/description.pdf -------------------------------------------------------------------------------- /week4/8-Anagrams/maycamp/gen_tests.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | import string 3 | from random import randint, choice, shuffle 4 | 5 | alphabet = list(string.ascii_lowercase) 6 | 7 | def get_two_permutations(length=20): 8 | a = [choice(alphabet) for _ in range(0, length)] 9 | b = a[:] 10 | shuffle(b) 11 | 12 | return (a, b) 13 | 14 | 15 | def corrupt(chars): 16 | return [chr(ord(x) + 1) for x in chars] 17 | 18 | 19 | def to_s(chars): 20 | return "".join(chars) 21 | 22 | 23 | NUMBER_OF_TESTS = 9 24 | 25 | for index in range(0, NUMBER_OF_TESTS): 26 | a, b = get_two_permutations() 27 | answer ="ANAGRAMS" 28 | file_name = "anagram00{}".format(index) 29 | 30 | if randint(0, 1) == 1: 31 | b = corrupt(b) 32 | answer = "NOT ANAGRAMS" 33 | 34 | with open(file_name + ".in", "w") as f: 35 | f.write(to_s(a) + " " + to_s(b)) 36 | 37 | with open(file_name + ".ans", "w") as f: 38 | f.write(answer) 39 | f.write("\n") 40 | 41 | -------------------------------------------------------------------------------- /week4/README.md: -------------------------------------------------------------------------------- 1 | # Generic Types & Generic Vectors 2 | 3 | ## Generic Types 4 | 5 | This is one very important concept in Java - the ability to give the user the ability (and power) to determine the type that he wants to keep inside the data structure. 6 | 7 | The best way to get a graps of generic types is to read the following documentation: 8 | 9 | ## Generic Vector 10 | 11 | [Here is an implementation of a generic Vector in Java.](https://github.com/HackBulgaria/Programming51-1/blob/master/week4/materials/Vector.java) 12 | 13 | ## Problems to solve 14 | 15 | There are plenty of problems, waiting for you to solve them, using the knowledge about the vector data structure. 16 | 17 | Once we have implemented it ourselves, we can use `java.util.Vector` - a generic `Vector` class, from the Java standard library. 18 | 19 | The probmes are: 20 | 21 | * [Music Organizer](1-Music-Organizer) 22 | * [Birthday Ranges](2-Birthday-Ranges) 23 | * [String Normalizer](3-String-Normalizer) 24 | * [Vector of Vectors](4-Vector-of-Vectors) 25 | * [Books of Lili](5-Books-of-Lili) 26 | * [Missing Students](6-Missing-Students) 27 | * [Max Span](7-Max-Span) 28 | * [Anagrams](8-Anagrams) 29 | 30 | -------------------------------------------------------------------------------- /week4/materials/Vector.java: -------------------------------------------------------------------------------- 1 | package week4; 2 | 3 | public class Vector { 4 | private T[] data; 5 | private int size = 0; 6 | private int capacity = 10; 7 | 8 | public Vector() { 9 | data = (T[]) new Object[capacity]; 10 | } 11 | 12 | public T get(int index) { 13 | return data[index]; 14 | } 15 | 16 | public void set(int index, T el) { 17 | data[index] = el; 18 | } 19 | 20 | public void add(T el) { 21 | if(size == capacity){ 22 | resize(); 23 | } 24 | data[size] = el; 25 | size++; 26 | } 27 | 28 | public void insert(int index, T el){ 29 | 30 | } 31 | 32 | public void remove(int index){ 33 | 34 | } 35 | 36 | private void resize(){ 37 | capacity = capacity * 2 + 1; 38 | T[] temp = (T[])new Object[capacity]; 39 | for(int i=0;i` class that should look like this: 6 | 7 | ```java 8 | public class Node{ 9 | public T data; 10 | public Node next; 11 | 12 | public Node(T data){ 13 | this.data = data; 14 | next = null; 15 | } 16 | } 17 | ``` 18 | 19 | In `class LinkedList` you should define the following methods: 20 | 21 | * `public void add(T el)` method that adds an element to the end of the list 22 | * `public T get(int index)` method that returns the element on the `index` place 23 | * `public void set(int index, T el)` method that sets the element on the `index` place 24 | * `public void remove(int index)` method that removes the element on the `index` place (and connects the other elements) 25 | * `public void insert(int index, T el)` method that inserts an element `el` at the `index` place 26 | 27 | ## Here's the boilerplate for the class: 28 | 29 | ```java 30 | package week5; 31 | 32 | public class LinkedList { 33 | 34 | private Node head; 35 | private Node tail; 36 | 37 | public LinkedList(){ 38 | // Proper constructor 39 | } 40 | 41 | public void add(T el){ 42 | // Append an element to the end of the list 43 | } 44 | 45 | public T get(int index){ 46 | // Return the element at the index place 47 | return null; 48 | } 49 | 50 | public void set(int index, T el){ 51 | // Set the value at the index place 52 | } 53 | 54 | public void remove(int index){ 55 | // Remove an element form the index place 56 | } 57 | 58 | public void insert(int index, T el){ 59 | // Insert an element on the index place 60 | } 61 | 62 | public String toString(){ 63 | return "Implement toString() method"; 64 | } 65 | } 66 | ``` 67 | 68 | ## And the boilerplate for Main: 69 | 70 | ```java 71 | package week5; 72 | 73 | public class Main { 74 | 75 | public static void main(String[] args){ 76 | LinkedList l = new LinkedList<>(); 77 | for (int i = 10; i < 20; i++) { 78 | l.add(i); 79 | } 80 | l.insert(2, 5); 81 | l.remove(1); 82 | System.out.println(l); 83 | } 84 | } 85 | ``` 86 | 87 | This should print: 88 | 89 | ``` 90 | 10 5 12 13 14 15 16 17 18 19 91 | ``` -------------------------------------------------------------------------------- /week5/2-More-LinkedList/README.md: -------------------------------------------------------------------------------- 1 | # More Linked List methods 2 | 3 | In your `class LinkedList` where you should have an implementation of a LinkedList add the following methods: 4 | 5 | * `public void addFirst(T data)` - inserts an element at the beginning of the list 6 | * `public void addLast(T data)` - inserts an element at the end of the list 7 | * `public T getFirst()` - returns the first element of this list 8 | * `public T getLast()` - returns the last element of this list 9 | * `public boolean contains(T data)` - returns true if the list contains the specified element 10 | * `public int getElementIndex(T data)` - returns the index of the specified element. If the list does not contain the element - return -1; 11 | * `public int size()` - returns the size of the list 12 | 13 | ## Here is a boilerplate for the methods. You should copy-paste them into your LinkedList class: 14 | 15 | ```java 16 | public class LinkedList { 17 | 18 | // The rest of the List 19 | 20 | public void addFirst(T data) { 21 | // Insert the data at the beginning of the list 22 | } 23 | 24 | public void addLast(T data) { 25 | // Insert data at the end of the list 26 | } 27 | 28 | public T getFirst() { 29 | // Return the first element of the list 30 | return null; 31 | } 32 | 33 | public T getLast() { 34 | // Return the last element of the list 35 | return null; 36 | } 37 | 38 | public boolean contains(T data) { 39 | // Returns true if the list contains the element 40 | return false; 41 | } 42 | 43 | public int getElementIndex(T data) { 44 | // Returns the index of the specified element 45 | return 0; 46 | } 47 | 48 | public int size() { 49 | // Returns the size of the list 50 | return -1; 51 | } 52 | } 53 | ``` 54 | 55 | ## And a boilerplate for Main: 56 | 57 | ```java 58 | import java.util.Scanner; 59 | 60 | public class Main { 61 | 62 | public static void main(String[] args) { 63 | Scanner scanner = new Scanner(System.in); 64 | LinkedList l = new LinkedList<>(); 65 | int n; 66 | n = scanner.nextInt(); 67 | for (int i = 0; i < n; i++) { 68 | l.addLast(scanner.nextInt()); 69 | } 70 | 71 | l.insertAfter(3,scanner.nextInt()); 72 | 73 | l.remove(2); 74 | 75 | l.addFirst(scanner.nextInt()); 76 | 77 | int x = scanner.nextInt(); 78 | 79 | System.out.println(l.contains(x)); 80 | System.out.println(l.getFirst() + " " + l.getLast()); 81 | System.out.println(l); 82 | 83 | 84 | } 85 | 86 | } 87 | ``` 88 | 89 | ## Example: 90 | 91 | If you paste this into the console: 92 | ``` 93 | 5 94 | 10 23 4 15 90 100 120 112 95 | ``` 96 | 97 | The result should be: 98 | 99 | ``` 100 | false 101 | 120 90 102 | [120, 10, 23, 15, 100, 90] 103 | ``` -------------------------------------------------------------------------------- /week5/3-Static-LinkedList/README.md: -------------------------------------------------------------------------------- 1 | # Static Linked List 2 | 3 | Create a `class StaticLinkedList` and implement the following methods: 4 | 5 | * `public static LinkedList reverse(LinkedList l)` - returns a reversed list 6 | * `public static LinkedList sort(LinkedList l)` - returns a sorted list with the numbers from l 7 | * `public static LinkedList merge(LinkedList a, LinkedList b)` - returns a list that has all the numbers from a and b sorted 8 | 9 | ## A boilerplate for the class: 10 | 11 | ```java 12 | public class StaticLinkedList { 13 | 14 | public static LinkedList reverse(LinkedList l) { 15 | // Reverses the list 16 | return null; 17 | } 18 | 19 | 20 | public static LinkedList sort(LinkedList l) { 21 | // Sorts the list 22 | return null; 23 | } 24 | 25 | public static LinkedList merge(LinkedList a, LinkedList b) { 26 | // Merges a and b into one sorted list 27 | return null; 28 | } 29 | } 30 | ``` 31 | 32 | ## A boilerplate for main: 33 | 34 | ```java 35 | import java.util.Scanner; 36 | 37 | public class Main { 38 | 39 | public static void main(String[] args) { 40 | LinkedList l = new LinkedList<>(); 41 | LinkedList secondL = new LinkedList<>(); 42 | Scanner scanner = new Scanner(System.in); 43 | int n = scanner.nextInt(); 44 | for (int i = 0; i < n; i++) { 45 | l.add(scanner.nextInt()); 46 | } 47 | n = scanner.nextInt(); 48 | 49 | for (int i = 0; i < n; i++) { 50 | secondL.add(scanner.nextInt()); 51 | } 52 | 53 | 54 | System.out.println(StaticLinkedList.reverse(l)); 55 | System.out.println(StaticLinkedList.sort(l)); 56 | System.out.println(StaticLinkedList.merge(l,secondL)); 57 | } 58 | 59 | } 60 | ``` 61 | 62 | ## Example: 63 | 64 | Input: 65 | ``` 66 | 3 67 | 4 10 5 68 | 6 69 | 2 3 -10 29 4 50 70 | ``` 71 | 72 | Output: 73 | 74 | ``` 75 | [5, 10, 4] 76 | [10, 5, 4] 77 | [50, 29, 10, 5, 4, 4, 3, 2, -10] 78 | ``` -------------------------------------------------------------------------------- /week5/4-Gas-Stations/README.md: -------------------------------------------------------------------------------- 1 | # Gas Stations 2 | 3 | We are implementing a smart GPS software. 4 | 5 | - We are taking a long trip from Sofia to Vratsa and we know the distance between the two cities.It is a positive integer and we mark it as ``tripDistance``. 6 | 7 | - We know how much our car can ride with a full tank of gas. It is a positive integer in kilometers. We mark it as ``tankSize``. 8 | 9 | - We know how much gas stations are there on our roude. It is a positive intager and we mark it as ``gasStationsCount`` 10 | 11 | - We have a list of gas stations. We know the distance between Sofia and the current gas station. ``gasStations = [50, 80, 110, 180, 220, 290]`` The list is sorted! 12 | 13 | By using this information we will implement a function that returns the shortest list of gas stations that we have to visit in order to travel from Sofia to Vratsa. Know that are allways starting with a full tank. 14 | 15 | ##Here is a boilerplate class ready to take console input: 16 | 17 | ```java 18 | public class GPS { 19 | 20 | public static Vector getGasStations(int tripDistance, int tankSize, Vector gasStations) { 21 | return new Vector(); 22 | } 23 | 24 | public static void main(String[] args) { 25 | Scanner scanner = new Scanner(System.in); 26 | int tripDistance = scanner.nextInt(); 27 | int tankSize = scanner.nextInt(); 28 | 29 | int gasStationsCount = scanner.nextInt(); 30 | Vector gasStations = new Vector(); 31 | 32 | for (int i = 0; i < gasStationsCount; i++) { 33 | gasStations.add(scanner.nextInt()); 34 | } 35 | 36 | Vector result = getGasStations(tripDistance, tankSize, gasStations); 37 | 38 | for (int i = 0; i < result.size(); i++) { 39 | System.out.println(result.get(i)); 40 | } 41 | 42 | } 43 | } 44 | ``` 45 | 46 | ## Example 47 | For input 48 | ``` 49 | 320 50 | 90 51 | 6 52 | 50 53 | 80 54 | 140 55 | 180 56 | 220 57 | 290 58 | ``` 59 | 60 | Give the following output: 61 | 62 | ``` 63 | 80 64 | 140 65 | 220 66 | 290 67 | ``` 68 | -------------------------------------------------------------------------------- /week5/5-Another-LinkedList/README.md: -------------------------------------------------------------------------------- 1 | # Another LinkedList 2 | 3 | In your `class LinkedList` where you should have an implementation of a LinkedList add the following methods: 4 | 5 | * `public T[] toArray()` - returns the linked list in the form of an array 6 | * `public void addArray(T[] arr)` - appends all the elements of the array to the end of this list 7 | * `public void clear()` - removes all the elements from the list 8 | * `public void insertListAfter(int index, LinkedList l)` - inserts all the elements from `l` into the List after the index element. Use a very fast algorithm. 9 | * `public void insertListBefore(int index, LinkedList l)` - inserts all the elements from `l` into the List before the index element 10 | 11 | ## Here's a boilerplate for the methods in the class: 12 | 13 | ```java 14 | public T[] toArray() { 15 | // Returns an array with all the elements in the list 16 | } 17 | 18 | public void addArray(T[] arr) { 19 | // Appends the array at the end of the list 20 | } 21 | 22 | public void insertListAfter(int index, LinkedList l) { 23 | // Inserts all the elements of l after the index element 24 | } 25 | public void insertListBefore(int index, LinkedList l){ 26 | // Inserts all the elements of l before the index element 27 | } 28 | ``` 29 | 30 | Here's the boilerplate for main: 31 | 32 | ```java 33 | import java.util.Scanner; 34 | 35 | public class Main { 36 | 37 | public static void main(String[] args) { 38 | Scanner scanner = new Scanner(System.in); 39 | LinkedList list1 = new LinkedList<>(); 40 | LinkedList list2 = new LinkedList<>(); 41 | Integer [] arr; 42 | int n = scanner.nextInt(); 43 | for (int i = 0; i < n; i++) { 44 | list1.addLast(scanner.nextInt()); 45 | } 46 | n = scanner.nextInt(); 47 | arr = new Integer[n]; 48 | for (int i = 0; i < n; i++) { 49 | arr[i] = scanner.nextInt(); 50 | } 51 | list2.addArray(arr); 52 | list1.insertListBefore(0, list2); 53 | list1.insertListAfter(0, list2); 54 | 55 | System.out.println(list1); 56 | } 57 | } 58 | ``` 59 | 60 | ## Example: 61 | 62 | Example input: 63 | ``` 64 | 3 65 | 1 5 10 66 | 2 67 | 3 7 68 | ``` 69 | 70 | Should print: 71 | ``` 72 | [3, 3, 7, 7, 1, 5, 10] 73 | ``` -------------------------------------------------------------------------------- /week5/6-Count-The-Words/README.md: -------------------------------------------------------------------------------- 1 | # Word counter 2 | 3 | You are given a rectangular table filled with characters and a `word`. 4 | Your task is to count the occurences of the `word` in the table. The word can be found horizontaly, vertically and across both left to right and right to left. 5 | 6 | ## For example: 7 | 8 | find the word `ivan` in the table: 9 | 10 | | i | v | a | n | 11 | |--- |--- |--- |--- | 12 | | e | **v** | **n** | h | 13 | | i | n | **a** | v | 14 | | m | v | **v** | **n** | 15 | | q | r | **i** | t | 16 | 17 | Result: 18 | ``` 19 | 3 20 | ``` 21 | 22 | Here's a boilerplate for the main method: 23 | 24 | ```java 25 | import java.util.Scanner; 26 | 27 | public class Main { 28 | 29 | public static void main(String[] args) { 30 | Scanner scanner = new Scanner(System.in); 31 | String searched = scanner.next(); 32 | char [][] map; 33 | int n,m; 34 | n = scanner.nextInt(); 35 | m = scanner.nextInt(); 36 | map = new char[n][m]; 37 | for (int i = 0; i < n; i++) { 38 | for (int j = 0; j < m; j++) { 39 | map[i][j] = scanner.next().charAt(0); 40 | } 41 | } 42 | 43 | // Print result here 44 | } 45 | } 46 | ``` 47 | 48 | ## Example inputs: 49 | 50 | ### Example 1: 51 | 52 | ``` 53 | ivan 54 | 5 4 55 | i v a n 56 | e v n h 57 | i n a v 58 | m v v n 59 | q r i t 60 | ``` 61 | 62 | Should print: 63 | 64 | ``` 65 | 3 66 | ``` 67 | 68 | ### Example 2: 69 | 70 | ``` 71 | actually 72 | 8 15 73 | i v a n q h r e z g t z o y m 74 | e v n h t r x e k y d a i l c 75 | i a c t u a l l y m c x r l e 76 | m v c n p u a m n t l u e a a 77 | q r i t w e a q u p r x t u z 78 | p e a c t u a l l y w p y t m 79 | o y h t r e l u f p q n z c s 80 | p a c t u a l l y u r e q a r 81 | ``` 82 | 83 | Should print: 84 | 85 | ``` 86 | 4 87 | ``` 88 | 89 | ### Example 3: 90 | 91 | ``` 92 | madam 93 | 8 12 94 | z v a n q h r e z g t z 95 | e v m h t r x e k y m a 96 | i a c a u a l l y a c x 97 | m v c n d u a m d t l u 98 | q t i t w a a a u p r x 99 | p e m a d a m l l y w p 100 | o y h t e e l u f p q n 101 | p a c t u a l l y u r e 102 | ``` 103 | 104 | Should print: 105 | 106 | ``` 107 | 3 108 | ``` 109 | Note: this should print 3, not 6 110 | -------------------------------------------------------------------------------- /week5/README.md: -------------------------------------------------------------------------------- 1 | # Linked Lists 2 | 3 | This week we are going to take a look at another data structure - the [Linked List](https://en.wikipedia.org/wiki/Linked_list) 4 | -------------------------------------------------------------------------------- /week6/1-Landscape/README.md: -------------------------------------------------------------------------------- 1 | # Landscape 2 | 3 | We are going to make a beautiful, console-based 2D landscape that looks like that: 4 | 5 | ``` 6 | --~/\!/\-- 7 | ``` 8 | 9 | Our landscape will consist of tiles, and each tile is represented by a String. 10 | 11 | You have the following interface: 12 | 13 | ```java 14 | public interface Tile { 15 | public String render(); 16 | } 17 | ``` 18 | 19 | Implement that interface with the following tiles: 20 | 21 | * `Land` should render as the `--` string. 22 | * `Water` should render as the `~~` string. 23 | * `Mountain` should render as the `/\` string. 24 | * `Forest` should render as the `!!` string. 25 | 26 | When you have the 4 classes ready, implement a class called `Landscape` which should have: 27 | 28 | * `addTile(Tile t)` method - adds tile to a list. 29 | * `String renderAll()` method - renders all tiles, concats them and returns the resulted String. 30 | 31 | ## Example 32 | 33 | Lets have a `Main` class: 34 | 35 | ```java 36 | public class Main { 37 | public static void main(String[] args) { 38 | Landscape landscape = new Landscape(); 39 | 40 | landscape.addTile(new Land()); 41 | landscape.addTile(new Water()); 42 | landscape.addTile(new Mountain()); 43 | landscape.addTile(new Forest()); 44 | 45 | System.out.println(landscpape.renderAll()); // --~~/\!! 46 | } 47 | } 48 | ``` 49 | -------------------------------------------------------------------------------- /week6/2-Zombie-Apocalypse/README.md: -------------------------------------------------------------------------------- 1 | # Zombie Apocalypse 2 | 3 | The time has come. Zombies are everywhere. It is the zombie apocalypse! 4 | 5 | You are in a room with zombies. You grab a weapon and start to fight them. 6 | 7 | And since you are a Java programmer, you fight them by writing a Java program to fight them. 8 | 9 | You have an interface for a weapon: 10 | 11 | ```java 12 | public interface Weapon { 13 | public int getDamage(); 14 | public int getDurability(); 15 | public String getType(); // can return "TO_SMASH" or "TO_SHOOT" 16 | } 17 | ``` 18 | 19 | The possible weapons around you are: 20 | 21 | * An `Axe` with damage of 10 and durability of 50. 22 | * A `Shotgun` with damage of 25 and durability of 10. 23 | * An `AncientSword` with damage of 50 and durability of 10. 24 | * A `Revolver` with damage of 15 and durability of 6. 25 | 26 | ## Durability, damage and types 27 | 28 | Depending on the weapon type, you have the following fighting dynamics: 29 | 30 | ### TO_SMASH weapons 31 | 32 | Weapons with type `TO_SMASH` lose their durability, everytime you hit a zombie. 33 | 34 | Once the durability of the weapon goes below 50% of the original durability value, the weapon deals 1 damage less with each hit. 35 | 36 | **For example** if you have `Axe` with damage = 10 and durability = 4 we have: 37 | 38 | * First hit, `durability = 4`. 4 >= 4 / 2, so the weapon deals 10 damage. Durability decreases to 3. 39 | * Second hit, `durability = 3`. 3 >= 4 / 2, so the weapon deals 10 damage. Durability decreases to 2. 40 | * Third hit, `durability = 2`. 2 >= 4 / 2, so the weapon deals 10 damage. Durability decreases to 1. 41 | * Fourth hit, `durability = 1`, 1 < 4 / 2, so the weapon deals 9 damage. Durability goes to 0 and stays there. 42 | 43 | Every hit that comes after this, will decrease the damage by 1, until the damage becomes 1. This is the lowest point for damage. 44 | 45 | ### TO_SHOOT weapons 46 | 47 | Weapons with type `TO_SHOOT` have bullets. The durability for that weapon is equal to the amount of bullets they have. 48 | 49 | Once the durability (bullets) goes to `0`, your can start smashing your enemies with the weapon (in cold blood). 50 | 51 | The damage of the weapon is always `1`, when you run out of bullets. 52 | 53 | ## Zombies 54 | 55 | There is a `Zombie` interface: 56 | 57 | ```java 58 | public interface Zombie { 59 | public int getTotalHealth(); 60 | public int getCurrentHealth(); 61 | public void hit(int damage); 62 | public boolean isDead(); 63 | } 64 | ``` 65 | 66 | Implement the interface with a proper class. This is your job. The name of the methods should give you a clue of their implementation. 67 | 68 | We need to be able to kill Zombies. 69 | 70 | ## The game 71 | 72 | Implement a Java program, that: 73 | 74 | * Reads one integer `n` from the input - this is the number of zombies in your room 75 | * Reads another integer `zombieHealth` from the input - this is going to be the total health of each zombie. 76 | * Reads one string `weapon` which is either one of the values `{"axe", "shotgun", "sword", "revolver"}` - this is the weapon you are going to have in the room. 77 | 78 | **On one line of the standard input, print the number of hits you have to make in order to kill all zombies in the room.** 79 | 80 | ## Example 81 | 82 | Input: 83 | 84 | ``` 85 | 2 10 86 | Axe 87 | ``` 88 | 89 | Output: 90 | 91 | ``` 92 | 2 93 | ``` 94 | 95 | We kill the first zombie (`damage = 10`, `health = 10`) with the first hit. We do the same with the second hit. 96 | 97 | --- 98 | 99 | Input: 100 | 101 | ``` 102 | 3 100 103 | sword 104 | ``` 105 | 106 | Output: 107 | 108 | ``` 109 | 7 110 | ``` 111 | 112 | * We kill the fist zombie with two hits. `Durability = 8` 113 | * We kill the second zombie with two hits. `Durability = 6` 114 | * We hit the third zombie once, `Durability = 5`. The next hit will deal `49` damage. So we need another hit, with `48` damage to kill it. 115 | * A total of `7` hits. 116 | -------------------------------------------------------------------------------- /week6/3-Labirinth/README.md: -------------------------------------------------------------------------------- 1 | # Labirinth 2 | 3 | You are in a labirinth and you have to find your way out. 4 | 5 | The labirinth looks similar to this: 6 | 7 | ``` 8 | # # # # # # # # 9 | # X . # . . . # 10 | # . . . . # . # 11 | # . . # . . . # 12 | # # . . . . # # 13 | # . . . # . . # 14 | # # # # # # ! # 15 | ``` 16 | 17 | The labirinth is a 2D array of chars - `char[][]` 18 | 19 | First, you are going to implement a program, that takes input from the console and moves you in the right direction. 20 | 21 | Your task is to get yourself out of the labirinth and reach the `!` char. 22 | 23 | **You are represnted by the `X` character.** 24 | 25 | ## The Labirinth class 26 | 27 | Implement a class called `Labirinth` that takes the labirnth array from the constructor. 28 | 29 | The class should have the following interface: 30 | 31 | * `String toString()` - returns a String, ready to be printed on the console. 32 | * `void print()` - prints the labirinth to the console 33 | * `boolean movePerson(Movement move)` - returns `true` if we can make the move, otherwise `false` 34 | 35 | This is it. 36 | 37 | Now, lets look into the `Movement` class. 38 | 39 | ## Movement 40 | 41 | The Movement class is going to be our main class, that we will inherit from: 42 | 43 | ```java 44 | public class Movement { 45 | public Pair move(Pair currentPosition) { 46 | System.out.println("Moving from the basic movement class."); 47 | return null; 48 | } 49 | } 50 | ``` 51 | 52 | We use the `Pair` class so we can store the `x` and `y` coordinates of our player in the labirinth ( the `X` ) 53 | Here's the implementation of the `Pair` class. It looks very similar to the `BoxWithTwoThings` class 54 | 55 | ```java 56 | public class Pair { 57 | private T first; 58 | private T second; 59 | 60 | public Pair(T first, T second) { 61 | this.first = first; 62 | this.second = second; 63 | } 64 | 65 | public T getFirst() { return first; } 66 | 67 | public T getSecond() { return second; } 68 | } 69 | ``` 70 | 71 | Now, subclass the `Movement` class in the following classes: 72 | 73 | * `MoveRight` 74 | * `MoveLeft` 75 | * `MoveDown` 76 | * `MoveUp` 77 | 78 | Each of the classes should give its own implementation to the `move` method - printing the direction that we are moving to. 79 | 80 | For example: 81 | 82 | ```java 83 | public class MoveRight extends Movement { 84 | public Pair move(Pair currentPosition) { 85 | System.out.println("Moving right!"); 86 | // This is just an example! The math here depends on yout implementation. 87 | Pair newPosition = new Pair<>(currentPosition.getFirst(), currentPosition.getSecond() + 1); 88 | 89 | return newPosition; 90 | } 91 | } 92 | ``` 93 | 94 | ## Taking input and moving us 95 | 96 | Here is an example of how to put everything together: 97 | 98 | Until you have reached the end of the labirinth: 99 | 100 | * Read one string from the input. 101 | * Print the result of the movement in the labirinth 102 | 103 | For example: 104 | 105 | ``` 106 | # # # # # # # # 107 | # X . # . . . # 108 | # . . . . # . # 109 | # . . # . . . # 110 | # # . . . . # # 111 | # . . . # . . # 112 | # # # # # # ! # 113 | 114 | Make your move: 115 | down 116 | 117 | # # # # # # # # 118 | # . . # . . . # 119 | # X . . . # . # 120 | # . . # . . . # 121 | # # . . . . # # 122 | # . . . # . . # 123 | # # # # # # ! # 124 | 125 | Make your move: 126 | down 127 | 128 | # # # # # # # # 129 | # . . # . . . # 130 | # . . . . # . # 131 | # X . # . . . # 132 | # # . . . . # # 133 | # . . . # . . # 134 | # # # # # # ! # 135 | 136 | Make your move: 137 | right 138 | 139 | # # # # # # # # 140 | # . . # . . . # 141 | # . . . . # . # 142 | # . X # . . . # 143 | # # . . . . # # 144 | # . . . # . . # 145 | # # # # # # ! # 146 | 147 | Make your move: 148 | down 149 | 150 | # # # # # # # # 151 | # . . # . . . # 152 | # . . . . # . # 153 | # . . # . . . # 154 | # # X . . . # # 155 | # . . . # . . # 156 | # # # # # # ! # 157 | 158 | Make your move: 159 | right 160 | 161 | # # # # # # # # 162 | # . . # . . . # 163 | # . . . . # . # 164 | # . . # . . . # 165 | # # . X . . # # 166 | # . . . # . . # 167 | # # # # # # ! # 168 | 169 | Make your move: 170 | right 171 | 172 | # # # # # # # # 173 | # . . # . . . # 174 | # . . . . # . # 175 | # . . # . . . # 176 | # # . . X . # # 177 | # . . . # . . # 178 | # # # # # # ! # 179 | 180 | Make your move: 181 | right 182 | 183 | # # # # # # # # 184 | # . . # . . . # 185 | # . . . . # . # 186 | # . . # . . . # 187 | # # . . . X # # 188 | # . . . # . . # 189 | # # # # # # ! # 190 | 191 | Make your move: 192 | down 193 | 194 | # # # # # # # # 195 | # . . # . . . # 196 | # . . . . # . # 197 | # . . # . . . # 198 | # # . . . . # # 199 | # . . . # X . # 200 | # # # # # # ! # 201 | 202 | Make your move: 203 | right 204 | 205 | # # # # # # # # 206 | # . . # . . . # 207 | # . . . . # . # 208 | # . . # . . . # 209 | # # . . . . # # 210 | # . . . # . X # 211 | # # # # # # ! # 212 | 213 | Make your move: 214 | down 215 | 216 | # # # # # # # # 217 | # . . # . . . # 218 | # . . . . # . # 219 | # . . # . . . # 220 | # # . . . . # # 221 | # . . . # . . # 222 | # # # # # # X # 223 | 224 | Congratulations! You have escaped the labirinth. 225 | 226 | Here are some aliens: (-(-(-_-)-)-) 227 | ``` 228 | -------------------------------------------------------------------------------- /week6/README.md: -------------------------------------------------------------------------------- 1 | # Week 6 - Interfaces, Inheritence, Abstract Classes & Polymorphism 2 | 3 | * We recommend reading - 4 | -------------------------------------------------------------------------------- /week7/1-Brackets/README.md: -------------------------------------------------------------------------------- 1 | # Brackets 2 | 3 | Here are some examples of correctly closed brackets: 4 | 5 | * `()` 6 | * `()[]` 7 | * `([])[]` 8 | * `{[]([]())}` 9 | 10 | And here are some examples of **incorrectly** closed brackets: 11 | 12 | * `(}` 13 | * `([)]` 14 | * `[({)]}` 15 | 16 | ## The task: 17 | 18 | The task is to create a method that recieves a `String` with brackets and returns true if these brackets are closed correctly. 19 | 20 | The method should look like this: 21 | 22 | ```java 23 | public class Brackets { 24 | public static boolean areCorrect(String brackets) { 25 | // Your implementation here 26 | return true; 27 | } 28 | } 29 | ``` 30 | 31 | Here's a boilerplate for the `main` function: 32 | 33 | ```java 34 | import java.util.Scanner; 35 | 36 | public class Main { 37 | 38 | public static void main(String[] args) { 39 | Scanner scanner = new Scanner(System.in); 40 | String brackets = scanner.next(); 41 | System.out.println(Brackets.areCorrect(brackets)); 42 | } 43 | } 44 | ``` 45 | 46 | ## Examples: 47 | 48 | ### Example 1: 49 | 50 | Input: 51 | 52 | ``` 53 | ()[{}{]} 54 | ``` 55 | 56 | Output: 57 | ``` 58 | false 59 | ``` 60 | 61 | ### Example 2: 62 | 63 | Input: 64 | ``` 65 | ()[{}[]][()] 66 | ``` 67 | 68 | Output: 69 | ``` 70 | true 71 | ``` 72 | 73 | ### Example 3: 74 | 75 | Input: 76 | ``` 77 | []{}()[]({}[) 78 | ``` 79 | Output: 80 | ``` 81 | false 82 | ``` 83 | 84 | ### Example 4: 85 | 86 | Input: 87 | ``` 88 | {[]{}()[]{}(){}} 89 | ``` 90 | 91 | Output: 92 | 93 | ``` 94 | true 95 | ``` 96 | -------------------------------------------------------------------------------- /week7/2-Ad-Bot/README.md: -------------------------------------------------------------------------------- 1 | # Ad Bot 2 | 3 | You are given the task to make a bot that displays AD messages in the subway. 4 | 5 | The add messages are strings that are given to you in a `String[]`. 6 | 7 | You must display the messages with a 1 second interval. 8 | 9 | There is a method in Java called `sleep(...)` that takes a `long` number as attribute and stops the program for the time given. Note that the time is given in miliseconds. One second equals 1000 miliseconds. So if you want to pause the program for 1 second you should call 'sleep(1000)'. 10 | You cant just call `sleep()`. As you already know everything in Java is an object, so you must call `sleep` on a class. 11 | The correct call is: 12 | 13 | ```java 14 | Thread.sleep(1000); 15 | ``` 16 | 17 | ## Boilerplates 18 | 19 | So here's a boilerplate for your `Main` class: 20 | 21 | ```java 22 | public class Main { 23 | 24 | public static void main(String[] args) throws InterruptedException { 25 | 26 | String[] s = { 27 | "Try the new invention! It gives you the ability to go from one room to another trough the wall. It is called a 'Door'!", 28 | "The new thing is out! It's called Sleeping! Sleeping helps you get energy for your next day! Try Sleeping TODAY!", 29 | "Here's another advertisement that I didn't want to think text for. Enjoy!", 30 | "Oh, and another AD. My, my! How exciting!" 31 | }; 32 | 33 | AdBot ad = new AdBot(s); 34 | for (int i = 0; i < 100; i++) { 35 | ad.showNextAd(); 36 | Thread.sleep(1000); 37 | } 38 | } 39 | } 40 | ``` 41 | 42 | You should implement the class `AdBot` that recieves the `String[]` with the ads and the method `public void showNextAd()` that prints the next ad. 43 | 44 | The ads should appear in the console one after another and when they end: they should start again from the first one. -------------------------------------------------------------------------------- /week7/3-String-Reverse/README.md: -------------------------------------------------------------------------------- 1 | # Reverse a String using Stack 2 | 3 | This one is easy. Reverse a String using only stack! 4 | 5 | Here is the boilerplate: 6 | 7 | ```java 8 | public class StringUtils { 9 | 10 | public static String reverseStringWithStack(String s) { 11 | return null; 12 | } 13 | } 14 | ``` 15 | -------------------------------------------------------------------------------- /week7/4-Stack-with-Queue/README.md: -------------------------------------------------------------------------------- 1 | # Implement a Stack using only Queue 2 | 3 | You have a Queue at your disposal. Implement the Stack data structure, using only the queue and the interface of it. 4 | 5 | ## Interface of Queue 6 | 7 | ```java 8 | public interface Queue { 9 | public T pop(); // removes the head and returns it 10 | public T peek(); // only returns the head without removing it 11 | public boolean isEmpty(); // returns true if empty 12 | public void push(T data); // pushes data into the queue 13 | } 14 | ``` 15 | 16 | ## The Stack class 17 | 18 | Here is the Stack class: 19 | 20 | ```java 21 | public class Stack { 22 | 23 | private Queue firstQueue = new LinkedList(); 24 | 25 | public T pop() { 26 | return null; 27 | } 28 | 29 | public T peek() { 30 | return null; 31 | } 32 | 33 | public boolean isEmpty() { 34 | return null; 35 | } 36 | 37 | public void push(T data) { 38 | return null; 39 | } 40 | } 41 | ``` 42 | 43 | You cannot use `LinkedList`. Use only `Queue`. 44 | -------------------------------------------------------------------------------- /week7/5-Positive-Negative/README.md: -------------------------------------------------------------------------------- 1 | # Positives and Negatives 2 | 3 | You are given an array of integers. 4 | 5 | Your job is to return a `Stack` of integers, where the negative integers are on top of the positive ones. Also, the negative integers should be sorted in **ascending order**, and the positive integers - in **descending order**. 6 | 7 | Here is the boilerplate: 8 | 9 | ```java 10 | public class PositivesAndNegatives { 11 | 12 | public Stack reorder(int[] numbers) { 13 | return null; 14 | } 15 | 16 | } 17 | 18 | ``` 19 | 20 | **For example:** 21 | 22 | If we have the following array `int[] n = {1, 2, 3, -10, -5, 8, -100}`, if we traverse the resulting Stack and print each number like that: 23 | 24 | ```java 25 | Stack result = reorder(n); 26 | 27 | while(!result.isEmpty()) { 28 | System.out.println(result.pop()); 29 | } 30 | ``` 31 | 32 | The output should be: 33 | 34 | ``` 35 | -100 36 | -10 37 | -5 38 | 8 39 | 3 40 | 2 41 | 1 42 | ``` 43 | 44 | ## Input 45 | 46 | Write a program that: 47 | 48 | * Reads one integer `n` 49 | * Reads `n` more integers. 50 | * Output the result of the reorder 51 | -------------------------------------------------------------------------------- /week7/6-Priority-Queue/README.md: -------------------------------------------------------------------------------- 1 | # Priority-Queue 2 | 3 | You are in a bus. You know that older people have bigger priority in a queue for the free seats. 4 | 5 | Implement a Priority Queue data structure with the following interface. 6 | 7 | ## Interface of Queue 8 | 9 | ```java 10 | public interface PriorityQueue { 11 | public int pop(); // remove and returns the oldest person. 12 | public int peek(); // only returns the oldest person without removing it 13 | public boolean isEmpty(); // returns true if empty 14 | public void push(int data); // pushes person into the queue 15 | } 16 | ``` 17 | 18 | **You can use any data structure you know!** 19 | 20 | ## Example 21 | 22 | ```java 23 | import java.util.Scanner; 24 | 25 | public class Main { 26 | 27 | public static void main(String[] args) { 28 | PriorityQueue queue = new PriorityQueue(); 29 | queue.push(60); 30 | queue.push(75); 31 | queue.push(85); 32 | queue.push(50); 33 | queue.push(56); 34 | 35 | System.out.println(queue.pop()); // 85 36 | System.out.println(queue.pop()); // 75 37 | System.out.println(queue.pop()); // 60 38 | } 39 | } 40 | ``` -------------------------------------------------------------------------------- /week7/7-Function-Calls/README.md: -------------------------------------------------------------------------------- 1 | # Function Calls 2 | 3 | We have defined 4 functions - f1, f2, f3 and f4 as follows: 4 | 5 | * `f1(x) = f2(x) + f3(x)` 6 | * `f2(x) = 2 * x` 7 | * `f3(x) = x + 1` 8 | * `f4(x) = 2 * f1(x)` 9 | 10 | All functions are working with integers. 11 | 12 | For example: 13 | 14 | ``` 15 | f1(1) = f2(1) + f3(1) = 2 * 1 + 2 + 1 = 4 16 | f2(1) = 2 * 1 = 2 17 | f3(1) = 1 + 1 = 2 18 | f4(1) = 2 * f1(1) = 10 19 | ``` 20 | 21 | **Function composition** is an expression of functions, separated by `.` 22 | 23 | For example: 24 | 25 | ``` 26 | f1 . f2 . f3 . f4 27 | ``` 28 | 29 | The idea is that **composition of functions** returns new function. Lets say: 30 | 31 | ``` 32 | h = f1 . f2 . f3 . f4 33 | ``` 34 | 35 | Now if we call `h` with some argument `x`, we are going to get the result: 36 | 37 | ``` 38 | h(x) = f1(f2(f3(f4(x)))) 39 | ``` 40 | 41 | You are about to write a Java program, that reads one function composition expression (of `f1`, `f2`, `f3`, `f4`) and value for the argument. 42 | 43 | The program should output the result of calling the function composition with the argument. 44 | 45 | ## Boilerplate 46 | 47 | ```java 48 | import java.util.Scanner; 49 | 50 | public class Parser { 51 | 52 | public static int evaluate(String formula, int x) { 53 | // implementation 54 | return x; 55 | } 56 | 57 | public static void main(String[] args) { 58 | Scanner scanner = new Scanner(System.in); 59 | 60 | String input = scanner.nextLine(); 61 | int x = scanner.nextInt(); 62 | 63 | System.out.println(evaluate(input, x)); 64 | 65 | } 66 | 67 | } 68 | ``` 69 | 70 | ## Examples 71 | 72 | * On the first line, read one `String`. Functions are going to be separated by `" . "`. 73 | * Read one more integer. 74 | 75 | **Input** 76 | 77 | ``` 78 | f2 79 | 1 80 | ``` 81 | 82 | **Output** 83 | 84 | ``` 85 | 2 86 | ``` 87 | 88 | This one is easy. We need only to call `f2(1)` which is equal to `1 + 1 = 2`. 89 | 90 | --- 91 | 92 | **Input** 93 | 94 | ``` 95 | f2 . f2 96 | 1 97 | ``` 98 | 99 | **Output** 100 | 101 | ``` 102 | 4 103 | ``` 104 | 105 | Now, we need to call `f2(f2(1))`, which is equal to `f2(2) = 4` 106 | 107 | --- 108 | 109 | **Input** 110 | 111 | ``` 112 | f1 . f2 . f3 . f4 113 | 5 114 | ``` 115 | 116 | **Output** 117 | 118 | ``` 119 | 199 120 | ``` 121 | 122 | This is a more complex example. We are going to expand it, so you get the idea: 123 | 124 | ``` 125 | We need to compute: f1(f2(f3(f4(5)))) 126 | 127 | First, we need to know the result of the most inner function: f4(5) 128 | 129 | f4(5) = 2 * f1(5) 130 | f1(5) = f2(5) + f3(5) 131 | f2(5) = 10 132 | f3(5) = 6 133 | 134 | => 135 | 136 | f1(5) = 10 + 6 = 16 137 | 138 | => 139 | 140 | f4(5) = 32 141 | 142 | Now the expression is: 143 | 144 | f1(f2(f3(32))) 145 | 146 | f3(32) = 33 147 | 148 | Now the expression is: 149 | 150 | f1(f2(33)) 151 | 152 | f2(33) = 66 153 | 154 | Now, we need to know f1(66) 155 | 156 | f1(66) = f2(66) + f3(66) 157 | f2(66) = 132 158 | f3(66) = 67 159 | 160 | f1(66) = 199 161 | ``` 162 | -------------------------------------------------------------------------------- /week7/8-Function-Calls-Extended/README.md: -------------------------------------------------------------------------------- 1 | # Function Calls with our Functions 2 | 3 | In order to solve that problem, it is a good idea to solve [Function Calls first](../7-Function-Calls) 4 | 5 | Now, in the previous problem, we had predefined functions. This is easy. **Now, we are going to read our functions from the input.** 6 | 7 | ## Function language 8 | 9 | We are going to use a very simple function language: 10 | 11 | * All functions are defined over the integers. 12 | * All functions are one liners - there are not going to be functions on multiple lines. 13 | * All functions are going to have **1 argument** 14 | * All functions can only do expressions involving valid integers, the argument and the `+` and `-` operations. Brackets, `*` and other operations are not in our language. 15 | * **Functions can include function calls to other functions.** 16 | * **In our function language everyting is separated by exactly one `" "`** You can count on that. 17 | 18 | Here are some example functions, that are valid: 19 | 20 | ``` 21 | f x = x + 1 22 | inc x = x + 1 23 | dec x = x - 1 24 | f2 x = 1 25 | f3 x = x + 1 - 2 26 | f4 x = inc(x) + inc(1) 27 | ``` 28 | 29 | Our functions have the following form: 30 | 31 | ``` 32 | = 33 | ``` 34 | 35 | * `` can be 1 or more characters from `[a-z]` 36 | * `` can be 1 or more characters from `[a-z]` 37 | * ` = | | ` where `|` means or. 38 | * `` are valid integers values. For example: `1`, `12`, `123` 39 | * `` are in the form `()` 40 | * `` can be only `+` and `-` 41 | 42 | ## The new problem 43 | 44 | Now, we have to make a Java program that: 45 | 46 | * Reads one integer `n` 47 | * On the next `n` lines - reads `n` fuctions 48 | * After this, reads one line consisting of function composition - lets call the composition `h` 49 | * On the last line, it reads an integer `x` 50 | 51 | The program should output the result of `h(x)`. 52 | 53 | ## Examples 54 | 55 | * The output is always going to be valid. 56 | * There won't be functions with the same name as the arguments. 57 | 58 | Watch carefully. There are a few corner cases here: 59 | 60 | Example input: 61 | 62 | ``` 63 | 2 64 | inc x = x + 1 65 | dec x = x - 1 66 | inc . inc . dec 67 | 5 68 | ``` 69 | 70 | The output should be: 71 | 72 | ``` 73 | 6 74 | ``` 75 | 76 | We are calling: 77 | 78 | ``` 79 | inc(inc(dec(5)) = inc(inc(4)) = inc(5) = 6 80 | ``` 81 | 82 | --- 83 | 84 | Another input: 85 | 86 | ``` 87 | 1 88 | const x = 1 89 | const . const . const 90 | 100 91 | ``` 92 | 93 | The output should be: 94 | 95 | ``` 96 | 1 97 | ``` 98 | 99 | Here `const` is a constant function. We can have all valid integer values for `x` but the result is always 1. 100 | 101 | --- 102 | 103 | Another input: 104 | 105 | ``` 106 | 1 107 | const x = 1 + 1 - 1 108 | const . const . const 109 | 100 110 | ``` 111 | 112 | The output should be: 113 | 114 | ``` 115 | 1 116 | ``` 117 | 118 | Again, we have a constant function but this time - there is an expression to be calculated. 119 | 120 | --- 121 | 122 | Another input: 123 | 124 | ``` 125 | 2 126 | inc x = x + 1 127 | dec x = inc(x) - 2 128 | dec . dec 129 | 2 130 | ``` 131 | 132 | The result should be: 133 | 134 | ``` 135 | 0 136 | ``` 137 | 138 | Now, the definition of `dec` uses `inc`. We must call `inc` with the `x` value passed to `dec`. 139 | 140 | --- 141 | 142 | Another input: 143 | 144 | ``` 145 | 2 146 | inc x = x + 1 147 | dec x = x - inc(0) 148 | dec . dec 149 | 2 150 | ``` 151 | 152 | The output should be: 153 | 154 | ``` 155 | 0 156 | ``` 157 | 158 | Now, we are calling `inc` again but this time with a specific value - `0`. This is not `x`! 159 | 160 | -------------------------------------------------------------------------------- /week7/8-Function-Calls-Extended/solution/Context.java: -------------------------------------------------------------------------------- 1 | package com.hackbulgaria.programming51.week7.solutions; 2 | 3 | import java.util.HashMap; 4 | 5 | public class Context { 6 | public static HashMap allFunctions = new HashMap<>(); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /week7/8-Function-Calls-Extended/solution/Function.java: -------------------------------------------------------------------------------- 1 | package com.hackbulgaria.programming51.week7.solutions; 2 | 3 | import java.util.LinkedList; 4 | 5 | public class Function { 6 | private String fName; 7 | private String argumentName; 8 | private String source; 9 | private String fBody; 10 | 11 | public Function(String source) { 12 | // inc x = x + 1 13 | this.source = source; 14 | 15 | String[] fParts = source.split(" = "); 16 | fBody = fParts[1]; 17 | 18 | String[] fDefinitionParts = fParts[0].split(" "); 19 | fName = fDefinitionParts[0]; 20 | argumentName = fDefinitionParts[1]; 21 | } 22 | 23 | public String getName() { 24 | return fName; 25 | } 26 | 27 | public String getArgumentName() { 28 | return argumentName; 29 | } 30 | 31 | public int evaluate(int x) { 32 | // "5 + 2 - 3" 33 | // ["5", "+", "2", "-", "3"] 34 | String[] expressionParts = fBody.split(" "); 35 | LinkedList operations = new LinkedList<>(); 36 | LinkedList values = new LinkedList<>(); 37 | 38 | for(String token: expressionParts) { 39 | if(isOperation(token)) { 40 | operations.add(token); 41 | } 42 | 43 | if(isStringInteger(token)) { 44 | values.add(Integer.parseInt(token)); 45 | } 46 | 47 | if(token.equals(argumentName)) { 48 | values.add(x); 49 | } 50 | 51 | if(isFunction(token)) { 52 | values.add(callOtherFunction(token, x)); 53 | } 54 | 55 | } 56 | 57 | while(!operations.isEmpty()) { 58 | String op = operations.removeFirst(); 59 | int left = values.removeFirst(); 60 | int right = values.removeFirst(); 61 | int result = 0; 62 | 63 | switch (op) { 64 | case "+": 65 | result = left + right; 66 | break; 67 | case "-": 68 | result = left - right; 69 | } 70 | 71 | values.addFirst(result); 72 | } 73 | 74 | return values.removeFirst(); 75 | } 76 | 77 | public String toString() { 78 | return source; 79 | } 80 | 81 | private int callOtherFunction(String token, int x) { 82 | // inc(x) 83 | // inc(1) 84 | 85 | String[] fParts = token.split("\\("); 86 | 87 | String fName = fParts[0]; 88 | String argument = fParts[1].substring(0, fParts[1].length() - 1); 89 | int argumentValue = 0; 90 | 91 | if(argument.equals(argumentName)) { 92 | argumentValue = x; 93 | } else if(isStringInteger(argument)) { 94 | argumentValue = Integer.parseInt(argument); 95 | } 96 | 97 | return Context.allFunctions.get(fName).evaluate(argumentValue); 98 | } 99 | 100 | private boolean isFunction(String token) { 101 | // Чрез регулярен израз. 102 | 103 | int indexOfOpenBracket = token.indexOf("("); 104 | int indexOfCloseBracket = token.indexOf(")"); 105 | 106 | if(indexOfOpenBracket == -1 || indexOfCloseBracket == -1) { 107 | return false; 108 | } 109 | 110 | return indexOfOpenBracket < indexOfCloseBracket; 111 | } 112 | 113 | // "5", "555", "12310", но не и "abc" 114 | private boolean isStringInteger(String token) { 115 | for(char ch: token.toCharArray()) { 116 | if(!Character.isDigit(ch)) { 117 | return false; 118 | } 119 | } 120 | 121 | return true; 122 | } 123 | 124 | private boolean isOperation(String token) { 125 | return (token.equals("+") || token.equals("-")); 126 | } 127 | } 128 | 129 | -------------------------------------------------------------------------------- /week7/8-Function-Calls-Extended/solution/FunctionCallsExtended.java: -------------------------------------------------------------------------------- 1 | package com.hackbulgaria.programming51.week7.solutions; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | public class FunctionCallsExtended { 7 | 8 | public static void main(String[] args) { 9 | Scanner scanner = new Scanner(System.in); 10 | 11 | int fCount = Integer.parseInt(scanner.nextLine()); 12 | ArrayList allFunctions = new ArrayList<>(); 13 | 14 | for(int i = 0; i < fCount; i++) { 15 | String nextF = scanner.nextLine(); 16 | Function f = new Function(nextF); 17 | 18 | allFunctions.add(f); 19 | Context.allFunctions.put(f.getName(), f); 20 | } 21 | 22 | String compositionInput = scanner.nextLine(); 23 | FunctionComposition composition = new FunctionComposition(compositionInput); 24 | 25 | int x = scanner.nextInt(); 26 | 27 | System.out.println(composition.evaluate(x)); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /week7/8-Function-Calls-Extended/solution/FunctionComposition.java: -------------------------------------------------------------------------------- 1 | package com.hackbulgaria.programming51.week7.solutions; 2 | 3 | import java.util.Stack; 4 | 5 | public class FunctionComposition { 6 | 7 | private Stack callStack = new Stack<>(); 8 | 9 | public FunctionComposition(String composition) { 10 | 11 | String[] functions = composition.split(" . "); 12 | 13 | for(String f: functions) { 14 | callStack.add(Context.allFunctions.get(f)); 15 | } 16 | } 17 | 18 | public int evaluate(int x) { 19 | while(!callStack.isEmpty()) { 20 | Function f = callStack.pop(); 21 | System.out.println("Evaluating function: " + f.getName() + "(" + x + ")"); 22 | x = f.evaluate(x); 23 | } 24 | 25 | return x; 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /week7/materials/Zombie/AncientSword.java: -------------------------------------------------------------------------------- 1 | package week7; 2 | 3 | public class AncientSword extends ToSmash { 4 | public AncientSword() { 5 | super(50,10); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /week7/materials/Zombie/Axe.java: -------------------------------------------------------------------------------- 1 | package week7; 2 | 3 | public class Axe extends ToSmash{ 4 | public Axe() { 5 | super(10, 50); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /week7/materials/Zombie/CasualZombie.java: -------------------------------------------------------------------------------- 1 | package week7; 2 | 3 | public class CasualZombie implements Zombie { 4 | 5 | private int health; 6 | 7 | public CasualZombie(int health) { 8 | this.health = health; 9 | } 10 | 11 | 12 | public int getTotalHealth() { 13 | return health; 14 | } 15 | 16 | public int getCurrentHealth() { 17 | return health; 18 | } 19 | 20 | public void hit(int damage) { 21 | health -= damage; 22 | if(health <= 0) { 23 | health = 0; 24 | } 25 | } 26 | 27 | public boolean isDead() { 28 | return health == 0; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /week7/materials/Zombie/Main.java: -------------------------------------------------------------------------------- 1 | package week7; 2 | 3 | import java.util.Scanner; 4 | import java.util.Vector; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Scanner scanner = new Scanner(System.in); 9 | 10 | int zAmbiesCount = scanner.nextInt(); 11 | int zAmbiesHealth = scanner.nextInt(); 12 | 13 | String weapon = scanner.next(); 14 | Weapon w; 15 | 16 | switch(weapon) { 17 | case "axe" : { w = new Axe(); break; } 18 | case "shotgun" : { w = new Shotgun(); break; } 19 | case "sword" : { w = new AncientSword(); break; } 20 | case "revolver" : { w = new Revolver(); break; } 21 | default : { System.out.println("Invalid weapon!"); return; } 22 | } 23 | 24 | Vector zombies = new Vector<>(); 25 | 26 | for (int i = 0; i < zAmbiesCount; i++) { 27 | zombies.add(new CasualZombie(zAmbiesHealth)); 28 | } 29 | 30 | int counter = 0; 31 | for (int i = 0; i < zombies.size(); i++) { 32 | Zombie current = zombies.get(i); 33 | while(!current.isDead()) { 34 | current.hit(w.hit()); 35 | counter++; 36 | } 37 | } 38 | System.out.println(counter); 39 | } 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /week7/materials/Zombie/Revolver.java: -------------------------------------------------------------------------------- 1 | package week7; 2 | 3 | public class Revolver extends ToShoot { 4 | public Revolver() { 5 | super(15, 6); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /week7/materials/Zombie/Shotgun.java: -------------------------------------------------------------------------------- 1 | package week7; 2 | 3 | public class Shotgun extends ToShoot{ 4 | public Shotgun() { 5 | super(25,10); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /week7/materials/Zombie/ToShoot.java: -------------------------------------------------------------------------------- 1 | package week7; 2 | 3 | public class ToShoot extends Weapon { 4 | 5 | public ToShoot(int damage, int durability) { 6 | super(damage, durability); 7 | } 8 | 9 | public int hit() { 10 | int dmg = damage; 11 | if(durability > 0) { 12 | durability--; 13 | } 14 | if(durability == 0) { 15 | damage = 1; 16 | } 17 | return dmg; 18 | } 19 | 20 | public String getType() { 21 | return "TO_SHOOT"; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /week7/materials/Zombie/ToSmash.java: -------------------------------------------------------------------------------- 1 | package week7; 2 | 3 | public class ToSmash extends Weapon { 4 | 5 | public ToSmash(int damage, int durability) { 6 | super(damage, durability); 7 | } 8 | 9 | public String getType() { 10 | return "TO_SMASH"; 11 | } 12 | 13 | public int hit() { 14 | int dmg = damage; 15 | if(durability > 0) { 16 | durability--; 17 | } 18 | if(durability <= maxDurability/2 && damage > 1) { 19 | damage--; 20 | } 21 | return dmg; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /week7/materials/Zombie/Weapon.java: -------------------------------------------------------------------------------- 1 | package week7; 2 | 3 | public abstract class Weapon { 4 | 5 | protected int damage; 6 | protected int durability; 7 | protected int maxDurability; 8 | 9 | public Weapon(int damage, int durability) { 10 | this.damage = damage; 11 | this.durability = durability; 12 | this.maxDurability = durability; 13 | } 14 | 15 | public int getDamage() { 16 | return damage; 17 | } 18 | 19 | public int getDurability() { 20 | return durability; 21 | } 22 | 23 | public abstract int hit(); 24 | 25 | public abstract String getType(); 26 | } 27 | -------------------------------------------------------------------------------- /week7/materials/Zombie/Zombie.java: -------------------------------------------------------------------------------- 1 | package week7; 2 | 3 | public interface Zombie { 4 | public int getTotalHealth(); 5 | public int getCurrentHealth(); 6 | public void hit(int damage); 7 | public boolean isDead(); 8 | } -------------------------------------------------------------------------------- /week8/1-TreeLevel/README.md: -------------------------------------------------------------------------------- 1 | # Tree Level 2 | 3 | You have the implementation of a Binary Tree. There are two tasks for you here: 4 | 5 | ## The First Task 6 | 7 | You must implement a function that returns the height of the tree. 8 | For example a tree like this: 9 | 10 | ``` 11 | 3 12 | / \ 13 | 2 5 14 | ``` 15 | 16 | Has a height of 2. 17 | 18 | And a tree like this: 19 | 20 | ``` 21 | 2 22 | \ 23 | 3 24 | \ 25 | 4 26 | \ 27 | 6 28 | ``` 29 | 30 | Has a height of 4. 31 | 32 | Implement the method `public int getTreeHeight()` (in the BST implementation that you will find in github) that returns the height of the tree given. 33 | 34 | ## The Second Task 35 | 36 | You must now implement the method `public LinkedList getTreeLevel(int level)` that returns a list with the elements of the tree on a certain level. 37 | For example: 38 | 39 | ``` 40 | 5 41 | / \ 42 | 3 7 43 | / \ \ 44 | 1 2 10 45 | ``` 46 | 47 | The third level of this tree is: 48 | 49 | ``` 50 | [1, 2, 10] 51 | ``` -------------------------------------------------------------------------------- /week8/2-Find-The-Panda/README.md: -------------------------------------------------------------------------------- 1 | # Find The Panda 2 | 3 | **After you get this done, try to rewrite it using recursion only!** 4 | 5 | You are in a maze and you are searching for your panda! 6 | 7 | Make a Java program that takes a maze, your position and a panda position. It should print ``true`` if there is a way from you to the panda and ``false`` if there is not a way. 8 | 9 | ## The Input 10 | First we take two numbers - The rows count and the cols count of the maze. 11 | 12 | 13 | Then we take a string that represents the maze: 14 | 15 | ``.`` - You can pass through this position. 16 | 17 | ``#`` - You can't pass through this position. 18 | 19 | ``U`` - This is your position 20 | 21 | ``P`` - This is the panda position 22 | 23 | 24 | ## Moves 25 | 26 | **You can move only up, down, left and right. You can't move diagonally!** 27 | 28 | 29 | ### Example 1: 30 | 31 | Input: 32 | 33 | ``` 34 | 10 35 | 10 36 | U..#...... 37 | ...#...... 38 | ##........ 39 | .......... 40 | #########. 41 | .......... 42 | .######### 43 | .........P 44 | .......... 45 | .......... 46 | ``` 47 | 48 | Output: 49 | ``` 50 | true 51 | ``` 52 | 53 | ### Example 2: 54 | 55 | Input: 56 | 57 | ``` 58 | 7 59 | 10 60 | ...#...U.. 61 | .......... 62 | .......... 63 | ...#...... 64 | ........#. 65 | .......#P# 66 | ...#....#. 67 | ``` 68 | 69 | 70 | Output: 71 | ``` 72 | false 73 | ``` -------------------------------------------------------------------------------- /week8/3-Lakes/README.md: -------------------------------------------------------------------------------- 1 | ##Lakes 2 | 3 | **We are living in 2D world and we are running out of water!** We are migrating to **KEPLER-186F**, the new planet that NASA discovered, but it is a slow migration because that planet is far far away. 4 | 5 | Luckily we still have some big lakes full of clean water. We need to know exactly how much water is in every lake. 6 | 7 | Write a program that takes the lake structure and calculates the amount of water there. 8 | 9 | **After you get this done, try to rewrite it using recursion only!** 10 | 11 | ## The Input 12 | 13 | We take a string that represents the structure of the lake. 14 | 15 | ``d`` - stands for **down** 16 | 17 | ``h`` - stands for **horizontal** 18 | 19 | ``u`` - stands for **up** 20 | 21 | ## Example 1 22 | 23 | For input 24 | ``` 25 | ddhhuu 26 | ``` 27 | 28 | We have lake structure like this: 29 | 30 | ![Example One](example1.jpg) 31 | 32 | **Every full square is 1000 liters of water!** 33 | 34 | So the output here obviously is: 35 | 36 | ``` 37 | 8000 38 | ``` 39 | 40 | ## Example 2 41 | 42 | Of course we can have much complex lakes like this: 43 | ``` 44 | ddhhddhuhhuuu 45 | ``` 46 | 47 | ![Example Two](example2.jpg) 48 | 49 | If you count carefully the answer here is: 50 | ``` 51 | 30000 52 | ``` 53 | 54 | ## Example 3 55 | 56 | We can have some small islands: 57 | ``` 58 | dddhhhuuhhuuuhdddduu 59 | ``` 60 | 61 | ![Example Two](example3.jpg) 62 | 63 | If you count carefully the answer here is: 64 | ``` 65 | 24000 66 | ``` -------------------------------------------------------------------------------- /week8/3-Lakes/example1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week8/3-Lakes/example1.jpg -------------------------------------------------------------------------------- /week8/3-Lakes/example2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week8/3-Lakes/example2.jpg -------------------------------------------------------------------------------- /week8/3-Lakes/example3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week8/3-Lakes/example3.jpg -------------------------------------------------------------------------------- /week8/4-Squares-In-The-Corner/README.md: -------------------------------------------------------------------------------- 1 | # Squares in the corner 2 | 3 | Make a Java2D application, that is `800x800` window. 4 | 5 | In the four corners of the window, make 4 different-colord squares. 6 | 7 | Your application should look something like this: 8 | 9 | ![](app.png) 10 | 11 | ## Boilerplate 12 | 13 | To get you started, have two files: `Application.java` and `Canvas.java`. 14 | 15 | Here is the boilerplate for `Application.java` (mind your own package): 16 | 17 | ```java 18 | import javax.swing.JFrame; 19 | 20 | public class Application extends JFrame { 21 | 22 | public Application() { 23 | add(new Canvas()); 24 | 25 | setResizable(false); 26 | pack(); 27 | 28 | setTitle("My application name"); 29 | 30 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 31 | setLocationRelativeTo(null); 32 | } 33 | 34 | public static void main(String[] args) { 35 | Application app = new Application(); 36 | app.setVisible(true); 37 | } 38 | } 39 | 40 | ``` 41 | 42 | Here is the boilerplate for `Canvas.java` (mind your own package): 43 | 44 | ```java 45 | package com.hackbulgaria.programming51.week8.corner; 46 | 47 | import java.awt.Color; 48 | import java.awt.Dimension; 49 | import java.awt.Graphics; 50 | import java.awt.Graphics2D; 51 | import java.awt.geom.Rectangle2D; 52 | 53 | import javax.swing.JPanel; 54 | 55 | public class Canvas extends JPanel { 56 | 57 | public static int WIDTH = 800; 58 | public static int HEIGHT = 800; 59 | 60 | public static int SQUARE_WIDTH = 400; 61 | 62 | public void paintComponent(Graphics g) { 63 | super.paintComponent(g); 64 | 65 | Graphics2D g2 = (Graphics2D) g; 66 | 67 | // an example 68 | Rectangle2D.Double upLeftSquare = new Rectangle2D.Double(0, 0, SQUARE_WIDTH, SQUARE_WIDTH); 69 | 70 | g2.setPaint(Color.GREEN); 71 | g2.fill(upLeftSquare); 72 | } 73 | 74 | public Canvas() { 75 | setPreferredSize(new Dimension(WIDTH, HEIGHT)); 76 | } 77 | } 78 | 79 | ``` 80 | -------------------------------------------------------------------------------- /week8/4-Squares-In-The-Corner/app.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week8/4-Squares-In-The-Corner/app.png -------------------------------------------------------------------------------- /week8/5-ChessBoard/README.md: -------------------------------------------------------------------------------- 1 | # Chessboard 2 | 3 | Make a Java2d application that resembles a chessboard. The pattern is more important than the count of the squares. 4 | 5 | Your application should look something like this: 6 | 7 | ![](app.png) 8 | 9 | You can reuse the boilerplate from the previous problem, but now you are on your own! 10 | -------------------------------------------------------------------------------- /week8/5-ChessBoard/app.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week8/5-ChessBoard/app.png -------------------------------------------------------------------------------- /week8/6-Triangles/README.md: -------------------------------------------------------------------------------- 1 | # Triangles 2 | 3 | Now, we are going to put our mouse in action. 4 | 5 | Watch [this video example](example.mp4) and you will understand what needs to be done. 6 | 7 | * Each 3 clicks should create a new triangle with different color each time (random color) 8 | * You need to call `repaint()` to call `paintComponent` again. This will do the magic 9 | * To listen for mouse input, your `Canvas` class should implement a `MouseListener` interface. The `mouseClicked` is the method you are looking for. **Examine the event argument.** 10 | 11 | Here is a simple boilerplate of the `Canvas.java`: 12 | 13 | ```java 14 | import java.awt.Dimension; 15 | import java.awt.Graphics; 16 | import java.awt.Graphics2D; 17 | import java.awt.event.MouseEvent; 18 | import java.awt.event.MouseListener; 19 | 20 | import javax.swing.JPanel; 21 | 22 | public class Canvas extends JPanel implements MouseListener { 23 | 24 | public static final int WIDTH = 800; 25 | public static final int HEIGHT = 800; 26 | 27 | 28 | public Canvas() { 29 | // very important here - addMouseListener(this) 30 | setPreferredSize(new Dimension(WIDTH, HEIGHT)); 31 | addMouseListener(this); 32 | } 33 | 34 | public void paintComponent(Graphics g) { 35 | super.paintComponent(g); 36 | Graphics2D g2 = (Graphics2D) g; 37 | 38 | // magic happens here 39 | } 40 | 41 | @Override 42 | public void mouseClicked(MouseEvent e) { 43 | // magic happens here 44 | 45 | // calling repaint is important 46 | repaint(); 47 | } 48 | 49 | @Override 50 | public void mousePressed(MouseEvent e) { 51 | } 52 | 53 | @Override 54 | public void mouseReleased(MouseEvent e) { 55 | // TODO Auto-generated method stub 56 | 57 | } 58 | 59 | @Override 60 | public void mouseEntered(MouseEvent e) { 61 | // TODO Auto-generated method stub 62 | 63 | } 64 | 65 | @Override 66 | public void mouseExited(MouseEvent e) { 67 | // TODO Auto-generated method stub 68 | 69 | } 70 | } 71 | 72 | ``` 73 | -------------------------------------------------------------------------------- /week8/6-Triangles/example.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackBulgaria/Programming51-1/77ad5379814574650142086155630b8f55aec785/week8/6-Triangles/example.mp4 -------------------------------------------------------------------------------- /week8/7-Bouncing-Ball/Application.java: -------------------------------------------------------------------------------- 1 | import javax.swing.JFrame; 2 | 3 | public class Application extends JFrame { 4 | 5 | public Application() { 6 | add(new Canvas()); 7 | 8 | setResizable(false); 9 | pack(); 10 | 11 | setTitle("Bouncing ball"); 12 | 13 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 14 | setLocationRelativeTo(null); 15 | } 16 | 17 | public static void main(String[] args) { 18 | Application app = new Application(); 19 | app.setVisible(true); 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /week8/7-Bouncing-Ball/Canvas.java: -------------------------------------------------------------------------------- 1 | import java.awt.Dimension; 2 | import java.awt.Graphics; 3 | import java.awt.Graphics2D; 4 | import java.awt.RenderingHints; 5 | import java.awt.Toolkit; 6 | import java.awt.geom.Ellipse2D; 7 | 8 | import javax.swing.JPanel; 9 | 10 | public class Canvas extends JPanel implements Runnable { 11 | 12 | private final int WIDTH = 800; 13 | private final int HEIGHT = 800; 14 | private final int RADIUS = 100; 15 | private final int DELAY = 25; 16 | 17 | private Ellipse2D.Double circle; 18 | private Thread animator; 19 | private Graphics2D g2; 20 | 21 | 22 | public void paintComponent(Graphics g) { 23 | super.paintComponent(g); 24 | g2 = (Graphics2D) g; 25 | 26 | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 27 | RenderingHints.VALUE_ANTIALIAS_ON); 28 | g2.fill(circle); 29 | 30 | Toolkit.getDefaultToolkit().sync(); 31 | } 32 | 33 | public Canvas() { 34 | setPreferredSize(new Dimension(WIDTH, HEIGHT)); 35 | setDoubleBuffered(true); 36 | circle = new Ellipse2D.Double(100, 100, RADIUS, RADIUS); 37 | 38 | } 39 | 40 | private void cycle() { 41 | // animation & collision detection happens here 42 | circle.x += 1; 43 | circle.y += 1; 44 | } 45 | 46 | @Override 47 | public void addNotify() { 48 | super.addNotify(); 49 | 50 | animator = new Thread(this); 51 | animator.start(); 52 | } 53 | 54 | @Override 55 | public void run() { 56 | 57 | long beforeTime, timeDiff, sleep; 58 | 59 | beforeTime = System.currentTimeMillis(); 60 | 61 | while (true) { 62 | 63 | cycle(); 64 | repaint(); 65 | 66 | timeDiff = System.currentTimeMillis() - beforeTime; 67 | sleep = DELAY - timeDiff; 68 | 69 | if (sleep < 0) { 70 | sleep = 2; 71 | } 72 | 73 | try { 74 | System.out.println("Sleeping for " + sleep); 75 | Thread.sleep(sleep); 76 | } catch (InterruptedException e) { 77 | System.out.println("Interrupted: " + e.getMessage()); 78 | } 79 | 80 | beforeTime = System.currentTimeMillis(); 81 | } 82 | } 83 | } 84 | 85 | -------------------------------------------------------------------------------- /week8/7-Bouncing-Ball/Canvas2.java: -------------------------------------------------------------------------------- 1 | package BouncingBall; 2 | 3 | import java.awt.Color; 4 | import java.awt.Dimension; 5 | import java.awt.Graphics; 6 | import java.awt.Graphics2D; 7 | import java.awt.RenderingHints; 8 | import java.awt.Toolkit; 9 | import java.awt.geom.Ellipse2D; 10 | 11 | import javax.swing.JPanel; 12 | 13 | public class Canvas extends JPanel implements Runnable { 14 | 15 | private final int WIDTH = 600; 16 | private final int HEIGHT = 600; 17 | private final int RADIUS = 100; 18 | private double mainSpeedX = 1; 19 | private double mainSpeedY = 2; 20 | private double speedX = 5; 21 | private double speedY = 10; 22 | private final int DELAY = 25; 23 | 24 | private Ellipse2D.Double circle; 25 | private Thread animator; 26 | private Graphics2D g2; 27 | 28 | 29 | public void paintComponent(Graphics g) { 30 | super.paintComponent(g); 31 | g2 = (Graphics2D) g; 32 | g2.setPaint(new Color( (int)((circle.y/HEIGHT)*255), (int)((circle.x/HEIGHT)*255), (int)((circle.y/HEIGHT)*255))); 33 | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 34 | RenderingHints.VALUE_ANTIALIAS_ON); 35 | g2.fill(circle); 36 | 37 | Toolkit.getDefaultToolkit().sync(); 38 | } 39 | 40 | public Canvas() { 41 | setPreferredSize(new Dimension(WIDTH, HEIGHT)); 42 | setDoubleBuffered(true); 43 | circle = new Ellipse2D.Double(100, 100, RADIUS, RADIUS); 44 | 45 | } 46 | 47 | private void cycle() { 48 | // animation & collision detection happens here 49 | if(circle.x + RADIUS >= WIDTH || circle.x <= 0) { 50 | speedX=mainSpeedX*2; 51 | speedX = -speedX; 52 | } 53 | if(circle.y + RADIUS >= HEIGHT || circle.y <= 0) { 54 | speedY = mainSpeedY*2; 55 | speedY = -speedY; 56 | } 57 | circle.x += speedX; 58 | circle.y += speedY; 59 | if(speedX > mainSpeedX) { 60 | speedX--; 61 | } /*else { 62 | speedX = mainSpeedX; 63 | }*/ 64 | if(speedY > mainSpeedY) { 65 | speedY--; 66 | } /*else { 67 | speedY = mainSpeedY; 68 | }*/ 69 | } 70 | 71 | @Override 72 | public void addNotify() { 73 | super.addNotify(); 74 | 75 | animator = new Thread(this); 76 | animator.start(); 77 | } 78 | 79 | @Override 80 | public void run() { 81 | 82 | long beforeTime, timeDiff, sleep; 83 | 84 | beforeTime = System.currentTimeMillis(); 85 | 86 | while (true) { 87 | 88 | cycle(); 89 | repaint(); 90 | 91 | timeDiff = System.currentTimeMillis() - beforeTime; 92 | sleep = DELAY - timeDiff; 93 | 94 | if (sleep < 0) { 95 | sleep = 2; 96 | } 97 | 98 | try { 99 | System.out.println("Sleeping for " + sleep); 100 | Thread.sleep(sleep); 101 | } catch (InterruptedException e) { 102 | System.out.println("Interrupted: " + e.getMessage()); 103 | } 104 | 105 | beforeTime = System.currentTimeMillis(); 106 | } 107 | } 108 | } -------------------------------------------------------------------------------- /week8/7-Bouncing-Ball/README.md: -------------------------------------------------------------------------------- 1 | # Bouncing Ball 2 | 3 | We have provided you with [`Application.java`](Application.java) and [`Canvas.java`](Canvas.java) that are set for basic animation. 4 | 5 | Your job is to extend the `Canvas` and do the following: 6 | 7 | * When the ball hits one of the 4 walls, it should bounce in the opposite direction 8 | * The ball is moving slowly now. Make it possible for the user to control the ball with the arrow keys (implement `KeyListener` interface). Of course, the ball should bounce back again, even if the user is controlling it. 9 | 10 | ## Understanding the animation 11 | 12 | Since we have not covered threads yet, we decided to give you the animation ready. But if you are interested in how things are working, here it is: 13 | 14 | * [The material that we have used is here](http://zetcode.com/tutorials/javagamestutorial/animation/) 15 | * There is an `Thread animator` property in `Canvas`. This `Thread` is going to do the animation. 16 | * Our class is a `Runnable`. Runnables can be given to Threads. Threads execute the `public void run() {}` method once. 17 | * The thing that loops and makes the animation is called a **game loop** 18 | * In our `run` method we make an infinite loop (in the separate thread) and calculate each game loop. 19 | * We call `cycle()` which changes the `x` and `y` values of the ball. 20 | * After `cycle()` we call `repaint()`, which will call `paintComponent` again. Then - we draw the circle on its new position. 21 | * In the `run` method, there is a calculation. We find out for how much time we should sleep, so the next cycle will be atleast that long as the previous one. This helps the animation to be smooth and not rushed. 22 | -------------------------------------------------------------------------------- /week8/materials/BST.java: -------------------------------------------------------------------------------- 1 | package BST; 2 | 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | 6 | public class BST> { 7 | private Node root; 8 | 9 | public BST() { 10 | root = null; 11 | } 12 | 13 | public boolean empty() { 14 | return root == null; 15 | } 16 | 17 | public void add(T data) { 18 | if (empty()) { 19 | root = new Node(data); // Случай че дървото е празно 20 | } else { 21 | if (data.compareTo(root.data) < 0) { 22 | // Добавяме в ляво 23 | if (root.left == null) { 24 | root.left = new Node(data); 25 | } else { 26 | addRecursive(data, root.left); 27 | } 28 | } else { 29 | // Добавяме в дясно 30 | if (root.right == null) { 31 | root.right = new Node(data); 32 | } else { 33 | addRecursive(data, root.right); 34 | } 35 | } 36 | } 37 | } 38 | 39 | private void addRecursive(T data, Node root) { 40 | if (data.compareTo(root.data) < 0) { // Проверка дали data е по-малко 41 | // Ще добавяме в ляво 42 | if (root.left == null) { 43 | root.left = new Node(data); 44 | } else { 45 | addRecursive(data, root.left);// Добавяме в лявото поддърво 46 | } 47 | } else { 48 | // Ще добавяме в дясно 49 | if (root.right == null) { 50 | root.right = new Node(data); 51 | } else { 52 | addRecursive(data, root.right); // Добавяме в дясното поддърво 53 | } 54 | } 55 | } 56 | 57 | private String toStringRecursive(Node root) { 58 | if (root == null) 59 | return ""; 60 | String str = ""; 61 | str += toStringRecursive(root.left); 62 | str += root.data + " "; 63 | str += toStringRecursive(root.right); 64 | return str; 65 | } 66 | 67 | public String toString() { 68 | String str = ""; 69 | str += toStringRecursive(root); 70 | return str; 71 | } 72 | 73 | public T findSmallest() { 74 | return findSmallestRecursive(root); 75 | } 76 | 77 | private T findSmallestRecursive(Node root) { 78 | if (root.left == null) { 79 | return root.data; 80 | } else { 81 | return findSmallestRecursive(root.left); 82 | } 83 | } 84 | 85 | public T findSmallest2() { 86 | Node current = root; 87 | while (current.left != null) { 88 | current = current.left; 89 | } 90 | return current.data; 91 | } 92 | 93 | public void printLevel(int level) { 94 | printLevelRecursive(level, root, 1); 95 | } 96 | 97 | private void printLevelRecursive(int level, Node root, int counter) { 98 | if (root == null) { 99 | return; 100 | } 101 | if (level == counter) { 102 | System.out.print(root.data + " "); 103 | } else { 104 | printLevelRecursive(level, root.left, counter + 1); 105 | printLevelRecursive(level, root.right, counter + 1); 106 | } 107 | } 108 | 109 | public int getLevel() { 110 | return getLevelRecursive(root); 111 | } 112 | 113 | private int getLevelRecursive(Node root) { 114 | if(root == null) { 115 | return 0; 116 | } 117 | int left = getLevelRecursive(root.left); 118 | int right = getLevelRecursive(root.right); 119 | 120 | if(left < right) return right + 1; 121 | return left + 1; 122 | } 123 | 124 | 125 | 126 | 127 | 128 | public void clear() { 129 | root = null; 130 | } 131 | 132 | public int getHeight() { 133 | return getHeightRecursive(root); 134 | } 135 | 136 | public int getHeightRecursive(Node root) { 137 | if(root == null) { 138 | return 0; 139 | } 140 | int left = getHeightRecursive(root.left); 141 | int right = getHeightRecursive(root.right); 142 | if(left < right) { 143 | return right + 1; 144 | } 145 | return left + 1; 146 | } 147 | 148 | public void balance() { 149 | LinkedList l = toList(); 150 | clear(); 151 | balanceRecursive(l); 152 | } 153 | 154 | public LinkedList toList() { 155 | LinkedList l = new LinkedList<>(); 156 | toListRecursive(root, l); 157 | return l; 158 | } 159 | 160 | private void toListRecursive(Node root, LinkedList l) { 161 | if(root == null) return; 162 | toListRecursive(root.left,l); 163 | l.add(root.data); 164 | toListRecursive(root.right,l); 165 | } 166 | 167 | private void balanceRecursive(List l) { 168 | if(l.isEmpty())return; 169 | 170 | int size = l.size(); 171 | int middle = size/2; 172 | if(size == 1) { 173 | add(l.get(0)); 174 | return; 175 | } 176 | 177 | add(l.get(middle)); 178 | balanceRecursive(l.subList(0, middle)); 179 | balanceRecursive(l.subList(middle + 1, size)); 180 | } 181 | } -------------------------------------------------------------------------------- /week8/materials/Labirinth.java: -------------------------------------------------------------------------------- 1 | 2 | public class Labirinth { 3 | private Player p; 4 | private boolean completed; 5 | private char[][] map = { { '#', '#', '#', '#', '#', '#', '#', '#' }, 6 | { '#', '.', '#', '.', '.', '.', '.', '#' }, 7 | { '#', '.', '.', '.', '#', '#', '#', '#' }, 8 | { '#', '.', '#', '.', '.', '#', '.', '#' }, 9 | { '#', '#', '#', '#', '.', '.', '.', '#' }, 10 | { '#', '.', '.', '.', '.', '#', '.', '#' }, 11 | { '#', '#', '#', '#', '#', '#', '!', '#' } }; 12 | 13 | public Labirinth() { 14 | completed = false; 15 | p = new Player(1, 1); 16 | p.setSymbol('@'); 17 | movePlayer(0,0); 18 | } 19 | 20 | public boolean movePerson(Movement direction) { 21 | Player dir = direction.move(); 22 | return movePlayer(dir.getX(),dir.getY()); 23 | } 24 | 25 | public boolean movePlayer(int x, int y) { 26 | if( p.getY() + y >= 0 && p.getY() + y < map.length 27 | && p.getX() + x >= 0 && p.getX() + x < map[0].length 28 | && map[p.getY() + y][p.getX() + x] != '#' 29 | ) { 30 | if(map[p.getY() + y][p.getX() + x] == '!') { 31 | completed = true; 32 | } 33 | map[p.getY()][p.getX()] = '.'; 34 | p.setX(p.getX() + x); 35 | p.setY(p.getY() + y); 36 | map[p.getY()][p.getX()] = p.getSymbol(); 37 | return true; 38 | } 39 | return false; 40 | } 41 | 42 | public void print() { 43 | System.out.println(this); 44 | } 45 | 46 | public String toString() { 47 | String str = ""; 48 | for (int i = 0; i < map.length; i++) { 49 | for (int j = 0; j < map[0].length; j++) { 50 | str += map[i][j] + " "; 51 | } 52 | str += '\n'; 53 | } 54 | return str; 55 | } 56 | 57 | public boolean isCompleted() { 58 | return completed; 59 | } 60 | } 61 | 62 | 63 | public class MoveUp implements Movement{ 64 | 65 | public Player move() { 66 | return new Player(0,-1); 67 | } 68 | 69 | } 70 | 71 | 72 | public class MoveLeft implements Movement{ 73 | 74 | public Player move() { 75 | return new Player(-1,0); 76 | } 77 | 78 | } 79 | 80 | public class MoveDown implements Movement { 81 | 82 | public Player move() { 83 | return new Player(0,1); 84 | } 85 | 86 | } 87 | 88 | public class MoveRight implements Movement { 89 | 90 | public Player move() { 91 | return new Player(1,0); 92 | } 93 | 94 | } 95 | 96 | public interface Movement { 97 | public Player move(); 98 | } 99 | 100 | public class Player { 101 | private int x; 102 | private int y; 103 | private char symbol; 104 | 105 | public Player(int x, int y) { 106 | this.x = x; 107 | this.y = y; 108 | this.setSymbol(symbol); 109 | } 110 | 111 | public int getX() { 112 | return x; 113 | } 114 | public int getY() { 115 | return y; 116 | } 117 | 118 | public void setX(int x) { 119 | this.x = x; 120 | } 121 | 122 | public void setY(int y) { 123 | this.y = y; 124 | } 125 | 126 | public char getSymbol() { 127 | return symbol; 128 | } 129 | 130 | public void setSymbol(char symbol) { 131 | this.symbol = symbol; 132 | } 133 | } 134 | 135 | 136 | 137 | public class Main { 138 | public static void main(String[] args) { 139 | Labirinth l = new Labirinth(); 140 | Scanner scanner = new Scanner(System.in); 141 | String direction; 142 | while(!l.isCompleted()) { 143 | System.out.println(l); 144 | System.out.println("Choose a direction to move: "); 145 | direction = scanner.next(); 146 | 147 | switch(direction) { 148 | case "u" : l.movePerson(new MoveUp()); break; 149 | case "d" : l.movePerson(new MoveDown()); break; 150 | case "l" : l.movePerson(new MoveLeft()); break; 151 | case "r" : l.movePerson(new MoveRight()); break; 152 | } 153 | } 154 | System.out.println(l); 155 | System.out.println("Mnogo si lud!"); 156 | } 157 | } -------------------------------------------------------------------------------- /week8/materials/Node.java: -------------------------------------------------------------------------------- 1 | package BST; 2 | 3 | public class Node { 4 | public T data; 5 | public Node left; 6 | public Node right; 7 | 8 | public Node(T data) { 9 | this.data = data; 10 | left = null; 11 | right = null; 12 | } 13 | } 14 | --------------------------------------------------------------------------------