Saturday, September 19, 2020
Writing Odia in LaTeX
Friday, August 7, 2020
Divisibility
One of my friends asked me to come up with a solution for this problem in Java/C#
public int isXDivisibleByY(int x, int y)
{
/*
The method should meet the following conditions:
1. Method returns 1, if X can be divided by Y without a remainder
2. Method returns 0, if X can't be divided by Y without a remainder
3. Solution must not include any conditional checks (if condition, equality operators, the ternary and null-coalescing operators etc.)
4. Solution must not include any exception handling (try-catch-finally)
5. It should work all kinds of inputs of x and y
*/
}
Challenges & Solutions
- I had failed to notice that there is no straight forward way to cast a boolean to int in Java. This is missing in Java because Java designers (correctly) recognized that the boolean / integer overloading in C and C++ was a significant source of errors. Source (https://stackoverflow.com/questions/16281760/why-doesnt-java-allow-casting-boolean-int). Thank God, there's this weird compareTo method in Boolean.
- Without a try-catch-Finally, Handling divide by Zero gracefully is not easy. However a simple cast of float to the divisor is handy. Thanks to the division rules https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.2
Final Solution by me:
public int isXDivisibleByY(int x, int y )
{
return 1 - Boolean.compare((x%(float)y) > 0, false) ;
}
Well, compare method may use conditionals internally but I guess, this is one possible solution without a visible conditional...
Next thing to try: Applying https://en.wikipedia.org/wiki/Divisibility_rule
Tuesday, August 14, 2012
XML Serialization in Android
Original JAXB generated class
|
Replace with SimpleXML annotation
|
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"book"
})
@XmlRootElement(name = "catalog")
|
@Root
|
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Review", propOrder = {
"value"
})
|
@Default
|
@XmlAttribute(name = "language")
protected String language;
|
@Attribute(name = "language", required=false)
protected String language;
Note: unless specified,
required=true. Therefore you have to make required=false
|
@XmlElement(name = "Date", required = true)
protected String date;
|
@Element(name = "Date", required = true)
protected String date;
|
@XmlValue
protected String value;
|
@Text(required=false)
|
@XmlElement(name = "UserReview", required = true)
protected
List<UserReview> userReview;
|
@ElementList(entry = "UserReview", inline = true)
protected
List<UserReview> userReview;
Note: ElementList is bit
tricky. ‘name’ won’t work, ‘entry’ works.
|
@XmlType(name = "Genre")
@XmlEnum
public enum Genre {
|
I don’t know how to do.
Used String instead
|
~Swarup
Wednesday, September 21, 2011
Write a C function that will print 1 to N one per each line on the stdout where N is a int parameter to the function. The function should not use while, for, do-while loops, goto statement, recursion, and switch statement.
Was able to do it.. therefore thought of sharing it too..
#include <setjmp.h>
#include <stdio.h>
void myfunc(int n)
{
jmp_buf ebuf;
int i;
int k = 1 ;
i = setjmp(ebuf);
if(i == 0)
{
longjmp(ebuf, k++);
}
else
{
if ( i <= n )
{
printf("%d\n", i);
longjmp(ebuf, k++);
}
}
}