text: tag:blogger.com,1999:blog-25978418152809984182017-12-06T13:03:44.681+01:00This Thread<i>tutorial-like examples and some informal chatting on C/C++/Java/Python software development (and more)</i>Manny Lowest Unique Number revisedGiven a list of numbers, find the pne that is unique and have the smallest value. I have just provided a python solution to this CodeEval problem, but there was something in it that bothered me. So here I provide an alternative solution. The soft spot in my previous solution was the combined sorting of the buffer counter dictionary added to a call to index() on the data list. I could live with Manny Lowest unique numberWe have a string in input containing numbers is the range [1 .. 9]. We want to get the index (following the unsettling Pascal habit of giving one for the first element) of the lowest unique number available, if such a beast exists, otherwise zero. This is the CodeEval problem #103. So, for instance, given these two sequences: 3 3 9 1 6 5 8 1 5 3 9 2 9 9 1 8 8 8 2 1 1 In the first case we should Manny Pass TriangleWe are given as input a sequence of integers that we should think representing a triangle. We should return the maximum value we can get adding all values from the top vertex of the triangle down to the bottom, chosing a path moving always downwards between adjacent elements. This is CodeEval problem #89. For example, if this is the triangle we get as input: 5 9 6 4 6 8 0 7 1 5 The expected Manny Query BoardWe have a square matrix of fixed size, and we receive a bunch of commands in input to operate on it. Two of them are about setting values on it, the other two perform a query and return an integer result. This problem is quite simple, more detail on CodeEval #87, still, I'm writing a few lines about it because it gave me a way to exercise with a couple of Python features. Mapping command names Manny Balanced SmileysThis problem is part of the family of the ones asking to check if parentheses in a sentence are balanced. See for instance the simpler matching brackets one. In this version, happy ':)' and sad ':(' smileys could be part of the the sentence, so that we have to think if a parentheses has to be considered in the balancing or skipped, being the representation of the mouth of the smiley. I found itManny a Stored Function from JDBCThe code we should write to call a stored procedure or function via JDBC is slightly different from the one we write to perform a SQL SELECT. A CallableStatement is used instead of a Statement plus a ResultSet to set the parameter, execute the call, and extract the result. To check it, I have firstly created a stored function in my Oracle 12 instance, on the HR schema. It takes a SQL CHAR in Manny on Oracle via JDBCOnce we decided how to establish a connection to the database, performing a SELECT on it is pretty simple. Here I'm going to use OracleDataSource as connection provider, using pure JDBC doesn't add any complexity. The only problem in this Java code is that is slightly boring. So boring that there are a few wrapper aiming at keeping the advantages of JDBC easing its usage. See for instance the Manny to Oracle via JDBCThere are a couple of ways to connect to a recent Oracle Database, going through the standard DriverManager class or the specific OracleDataSource one. Let's see both of them. And, when we are there, let's add also some consideration on connecting to MySql via DriverManager. It's a bit of an overkill, but I have written a little abstract class that is going to be the root of a hierarchy of Manny and configuring Oracle 12cIt was a while since the last time I have installed an instance of the Oracle database for local use. Nothing much has changed in the process, with the noticeable exception of the introduction of pluggable databases. Here I jot down a few remarks I made during the setup of the version 12 release 2. 1. In the page Typical Installation we have to specify the name for our pluggable database. Manny Minimum Path SumGiven a squared matrix of integers, find the minimal path sum from top-left to bottom-right, assuming that we can only move down or right. This is CodeEval problem #72. For example, give this matrix 4 6 2 8 We have just two possible paths: 4 -> 6 -> 8 and 4 -> 2 -> 8. It is immediate to see that the second one leads to the minimum sum of 14. The structure of the problem recalls a Dynamic Manny JdbcTemplate in Spitter Web AppIn the previous post I developed a CommandLineRunner Spring App to show how to use JdbcTemplate for simplify the JDBC access to an embedded instance of a H2 database. Here I port the changes to the Spittr Web App I have built up previously. Actually, there is not much to say. There is some inconsistency between the two apps that spiced up the process, but nothing really complicated. I'd say the Manny and JdbcTemplateLet's say that we want to write a Spring application that requires some simple database activity. We decide to use an embedded database, H2 and, instead of choosing a higher level persistency framework, we decide to go for the JDBC support, mediating it with JdbcTemplate, a Spring class aimed at simplifying its use. I create a Spring project using the STS wizard (File >> New >> Spring Starter Manny Word SearchGiven a board where in each cell there is a letter, check if word is in it. We could start anywhere, but we could move only horizontally and vertically. Each cell could be used only once. More details on the CodeEval page. The board is fixed, and looks like this: ABCE SFCS ADEE I have converted the examples provided in the problem in a python test case: class TestCodeEval(unittest.TestCase): Manny Levenshtein DistanceWe are given in input a short list of words, a separator, then another huge list of words. For each word in the first list, we have to return the numbers of elements in its "social network" from the second list. A word is "friend" of another one if their Levenshtein distance is one. Meaning, they have just one letter difference, as "aa" with "aaa", or "aaa" with "aba". Pay attention that the Manny Type AheadGiven the size of an n-gram in input, and its first n-1 components, we should write a function that returns our predicted possible last words, sorted by their calculated probability (from higher to lower) and then alphabetically. The solution assumes we generate our n-gram against the nursery rhyme Mary had a little lamb that we could safely hard-code in our program, stripping it from all Manny Cash RegisterGiven in input the price of a certain item and the cash that the client has passed to us to get it, we have to provide to the cashier a list of what he has to get back. More details in the problem page on CodeEval. We assume the register has an infinite amount of coins and bills in the given denominations: 'PENNY': .01, 'NICKEL': .05, 'DIME': .10, 'QUARTER': .25, 'HALF DOLLAR': .50, 'ONE': 1.00,Manny view on Spring error messagesWe have seen in the previous post how to let Spring validate the data coming from the view against a bean (in the sense of a plain Java one) defined in the model of our web app, thanks to the Java Validation protocol, in the Hibernate implementation. Now we see how to let the resulting view acknowledge the errors, so that the user could correct them. First thing, let's slightly change the Manny tha Java Validation APIThe user registration shown in the previous post has one very visible issue, there is no validation at all. The user could enter whatever value in each required field and the web app happily register it. Using Java Validation we could easily solve this problem. We need to add an implementation of the javax.validation validation API, the Hibernate validator is a good candidate. So I add its Manny registrationWe need to provide a way to let people register to our spittr web application, this is done here relying on the support offered by the Spring framework, that easily support the forms, giving a natural way for processing them. The Model A registered user is defined as a Spitter: public class Spitter { private Long id; private String username; private String password; private Manny a single SpittleIn our Spring Spitter Web App we want to display any single Spittle, just passing its id. This could be easily done accepting a request in the form of "spittles/show?id=42", following the style we have just used for getting a list of spittles, but we don't like it that much here, since we feel that "spittles/42" will be more expressive of the access to a resource. Good for us, the Spring Manny a paged list of spittlesAs next step in our little Spring Web App, we are going to add in the view a way to display spittles specifying the "end" id, using the standard convention that it is excluded from the interval, and how many of them we want to show. To do that, I firstly change the SpittleController so that its method mapped to the GET request method would accept the two parameters, giving them default values soManny Model to View through a Spring ControllerAnother improvement to our little Spittr Spring Web App. We'll fake a JDBC model, create a controller that would get some data from it and pass it to the view, a JSP page that uses EL to access the attribute. In the end we want to display to the user a very simple web page like this one: enter in the details of the JSP page, you can find it in my GitHub repository, I just mention the fact that Manny test for a Spring controllerHaving built a simple Spring Web App, now I want to test its only controller. In the previous post I have done it "by hand", running a browser on its url and verifying that I actually get the expected page, now I use the mock testing support provided by the Spring framework. Firstly I add to the pom file the Spring test dependency: <properties> <!-- ... --> <spring-test.version>4.3.0.RELEASE</Manny - a Spring Web ApplicationI am going to create a Spring Web Application using as IDE the Spring Tool Suite, friendly known as STS, version 3.8.4, without the Spring Boot support. To do that, we can theoretically use the wizard named Spring Legacy Project, but the support from Pivotal is not so strong as I expected and the result needs so many adjustments that in the end I opted to use the basic Maven Project wizard Manny 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 Manny