tag:blogger.com,1999:blog-25978418152809984182017-03-13T23:18:31.903+01:00This Thread<i>tutorial-like examples and some informal chatting on C/C++/Java/Python software development (and more)</i>Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.comBlogger750125tag:blogger.com,1999:blog-2597841815280998418.post-10404646616020591222017-03-13T23:16:00.000+01:002017-03-13T23:18:31.929+01:00CodeEval Following IntegerGiven a number, we should return its follower, accordingly to a weird rule explained on the CodeEval page of this problem. In a few words, we can only use the digits in the number itself, but we can add a zero if we need to.
The provided samples, that I converted to Python tests, give a better idea of our task:
def test_provided_1(self):
self.assertEqual(151, solution('115'))
def Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-53524463316092981402017-03-11T14:04:00.000+01:002017-03-11T14:06:55.763+01:00CodeEval Self Describing NumbersTell if the number passed in is self-describing, returning 1 in case of success. That number, also known as self-descriptive number, is such that it has in each digit, starting from the leftmost, the number of that digit in the number itself. If this definition looks confusing to you, you are not alone. Have a look to the CodeEval page for this problem, or to the tests here below.
Based on the Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-56759267659761950862017-03-10T12:15:00.000+01:002017-03-10T12:17:52.129+01:00CodeEval Happy NumbersCheck if a number is happy. Meaning that, adding up the squares of its digits we get one, or a number that, following the same recursice procedure, leads to one. A better description is on CodeEval. Now I see that I have already solved this problem a few years ago using C++ as implementation language. This time I used Python, and I have to say that the code looks more readable to me.
The core ofMannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-11069371853831753422017-03-09T23:34:00.001+01:002017-03-09T23:38:46.100+01:00CodeEval String ListIn this CodeEval problem we are given a number n and a string. We have to return, as a comma separated list, the sorted collection of all the possible words sized n from the letters in the string.
I have converted the given samples in python tests:
def test_provided_1(self):
self.assertEqual('a', solution('1,aa'))
def test_provided_2(self):
self.assertEqual('aa,ab,ba,bb', solution('2,abMannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-48350080035173430342017-03-08T18:13:00.000+01:002017-03-08T22:28:30.456+01:00CodeEval Message DecodingWe are given in input an encoded message, and we have to return it decoded. See the CodeEval page for this problem for more details.
Only one sample is presented, and I didn't bother to convert it to a test case, I just ran my Python script passing the provided input and I had visual check on the result. The point is the problem is not difficult, it just takes some time to get understood Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-62676494134068854932017-03-03T17:22:00.001+01:002017-03-03T21:51:05.110+01:00HackerRank Binary Search: Ice Cream ParlorGiven an integer, the budget for two ice creams, and a list of integer, the price of the available flavors, return the 1-based indeces for the two chosen flavors. As stated in the page problem, we should implement a binary search in our solution.
I converted the provided samples in python tests, and I added to them a couple more when I was thinking on possible algorithms:
def test_provided_1(Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-1867010522853363592017-03-02T18:16:00.000+01:002017-03-02T18:24:51.176+01:00The Spring AOP around adviceI am going to add a plain Spring component to my Spring Boot practice web application. The interesting part of it is that I am developing an AOP aspect that is designed to give a few advices in conjunction with a stated join point.
Here is the component, nothing fancy.
@Component
public class PopConcert implements Performance {
private static final Log log = LogFactory.getLog(Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-27508579432116761992017-03-02T15:49:00.000+01:002017-03-02T15:51:41.706+01:00HackerRank Merge Sort: Counting InversionsI found the description of this problem in its HackerRank page a bit misleading. Better trust its name instead. We have to count the inversions that occur when we run a merge-sort algorithm on list of positive integers.
So, basically, we have to increase a variable to keep track of the inversions and return it to the caller. If you know how to implement merge-sort, large part of the job is done.Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-68681903575917399842017-03-02T12:41:00.000+01:002017-03-02T12:48:38.992+01:00CodeEval Sum of integersGiven a list of integer numbers, return the largest sum in one of its subsequences of contiguous elements. CodeEval Sum of integers.
I have converted the given samples in python tests, then I added a few more cases to help me thinking about an algorithm.
def test_provided_1(self):
self.assertEqual(8, solution('-10,2,3,-2,0,5,-15')) # 1
def test_provided_2(self):
self.assertEqual(12, Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-81287664433136669462017-03-02T08:58:00.001+01:002017-03-02T09:00:45.232+01:00Plural Names IteratorGiven a file containing rules to convert a singular word in its plural, write a function that applies the best available rule for any passed name and return it in plural form.
The aim of this problem is the same of the one seen in the previous post, I am going to implement a similar plural() Python function that would get in input a word, as 'boy', and would return its plural form, like 'boys'.
Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-69072355417772740272017-03-01T11:39:00.000+01:002017-03-02T08:08:33.223+01:00Plural Names GeneratorGiven a file containing rules to convert a singular word in its plural, write a function that applies the best available rule for any passed name and return it in plural form.
My target is giving the user a function, named plural(), that gets in input a word and an optional file name for the rules to be applied (defaulted by the filename provided), and returns it as a plural word.
Here is a fewMannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-49369632194363338982017-02-28T16:07:00.000+01:002017-02-28T16:10:12.957+01:00Fibonacci IteratorWrite an iterator that gives back all the Fibonacci numbers up to a given value.
Use it to get the highest Fibonacci number less than 1000, and to check which if there is in that range a multiple of 12.
This problem is thought as a follow-up to the previous post, where we implemented the same functionality as a Python generator. I have extracted it from Dive into Python 3, section 7.5. The idea Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-37189010264713760192017-02-28T13:09:00.000+01:002017-02-28T15:38:21.759+01:00Fibonacci GeneratorWrite a function that generates Fibonacci numbers up to a given value.
Use it to get the highest Fibonacci number less than 1000, and to check if there is in that range a Fibonacci number that is multiple of 12.
You can solve this problem in a myriad of ways. However, the original idea was to push towards writing a Python generator. I extracted this example from Dive into Python 3, paragraph Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-23491989063626399912017-02-27T21:36:00.000+01:002017-02-27T21:42:38.084+01:00HackerRank Sorting: ComparatorWrite a comparator for a class Player that has, as data member, name (a string) and score (integer) so that sorting a list of players would put higher score on top. In case of a tie, names should be ordered in natural order.
I don't think this HackerRank problem was conceived with Python in mind, however there is some interest in implementing a solution in this language too.
As usual, I Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-9097462534479075592017-02-27T11:30:00.000+01:002017-02-27T11:42:31.986+01:00Phone NumbersWrite a function that checks if a given string represents a phone number.
This problem should look to you suspiciously similar to the previous one, that asked to check about Roman numbers. And actually I have extracted it from the same source, Dive into Python 3, chapter 5, that is about Regular Expressions.
So, the problem is defining a good pattern that matches the expected input. Here is a Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-25653305304079208242017-02-25T16:24:00.000+01:002017-02-25T16:35:31.943+01:00Roman NumbersWrite a function that checks if a given string represents a Roman number.
We can keep simple the solution to this problem using Regular Expressions. Using the python re library we just have to wrap a call to its search function, passing to it the input string and an appropriate pattern. If it does not find it, it returns None.
re.search(pattern, data)
The problem is identify the right pattern. Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-72061511136622509722017-02-25T12:22:00.000+01:002017-02-26T19:56:18.347+01:00Hackerrank Tries: ContactsWe are asked to provide a way to store contacts and check how many of them starts with a given prefix. As the name of this HackerRank problem suggests, we are gently pushed in the direction of implementing a trie. The issue is that one of the test cases that the solution has to pass to be accepted - #12 - is a bit too heavy for a plain python implementation.
My initial idea was to create a trie Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-16141651894467818142017-02-23T23:06:00.000+01:002017-02-26T20:19:00.031+01:00Tree traversalGiven a Binary Search Tree, give its node values following the in-order, pre-order, post-order, and level-order traversals, using both recursive and iterative algorithms. Actually, the HackerRank problem that gave me the idea for this post asks only to implement the level-order traversal algorithm, and you can write the code as you like, so probably in the iterative way, that comes more natural Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-83434798516354376612017-02-21T20:29:00.000+01:002017-02-26T20:00:28.135+01:00Hackerrank Heaps: Find the Running MedianGiven in input a stream of integers, calculate their running median. As the name of this HackerRank problem suggests, we should find a solution based on heaps data structures.
However, it is difficult to avoid the temptation of trying a different approach. Since I am using Python 3 as implementation language, the insort() function from the bisect library, makes the naive solution of sorting the Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-61285199455977973462017-02-20T17:14:00.000+01:002017-02-20T17:30:48.385+01:00Spring Prototype BeanBy default, the Spring framework creates beans as singleton. However we can instruct it to create a different bean each time a request is made specifying the prototype mode. Another couple of ways are available, to create a bean for each request or session.
I have written a simple JUnit test to see the difference between prototype and singleton.
I have created two empty classes, both of them Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-42598208825548094482017-02-20T09:41:00.000+01:002017-03-02T10:34:55.440+01:00CodeEval Reverse WordsGiven a string of words, write a function that returns them in reversed order. This is CodeEval problem #8 that has a trivial Python solution.
This is what we want to get, as a python test:
def test_provided_1(self):
self.assertEqual('World Hello', solution('Hello World'))
And the core of the solution is a mere one-liner:
' '.join(reversed(line.split()))
We split the line, getting a list of Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-26804861904141479312017-02-17T12:48:00.002+01:002017-02-17T12:59:05.351+01:00HackerRank Trees: Is This a Binary Search Tree?As the title suggest, the point of this hackerrank problem is checking if a tree is a BST.
Before being able to work on this problem, you should know what we are talking about. In a few words, a BST is a tree in which for each node is true that its left children are strictly smaller and the right one are strictly bigger.
In Python, if a node is represented in this way
class Node:
def Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-6937879965473255852017-02-16T22:56:00.000+01:002017-02-16T23:02:11.427+01:00Autowiring ambiguityI have a hierarchy of Spring Components rooted in the interface Dessert. Let's see what could go wrong when I try to autowire on them.
Here is the base interface:
public interface Dessert {
default double getPrice() {
return 0;
}
}
It has three empty implementing classes named Cake, Cookies, and IceCream. For instance, here is the third one:
@Component
public class IceCream Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-78349197506287892032017-02-16T13:51:00.000+01:002017-03-02T10:30:37.516+01:00CodeEval Prefix ExpressionsThe CodeEval problem #7 asks us to write a toy calculator for simple arithmetic expressions written in Polish notation. We expect only addictions, multipications, and divisions. And no inner expressions are allowed, meaning that we can safely expect to have in input firstly all the operators then operands, as integer numbers. We should give an integer answer, even though the internal process Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-91575246887761960752017-02-15T22:27:00.005+01:002017-02-26T20:10:30.392+01:00Hackerrank Queues: A Tale of Two StacksThis Hackerrank problem asks us to emulate a queue using two stacks. The rationale for this apparently weird data structure could be that we know for sure that we are going to have a data traffic in a sort of wave on the seaside fashion. We have a huge number of pushes with (about) no peek or pop, and then a huge number of peek or pop.
The architect should have calculate that it is not worthy to Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0