Pascal Triangle Find position x, y with Haskell function - haskell

I want to get 2 integers from an input like pascal 2 1. This input should be 2, because the list starts with x and y = 0. Also pos must be <= row and i don't want to use guards. My code looks like this:
pascal :: Int -> Int -> Int
pascal row pos
if row == 0 || pos == 0 then "1"
else if row > pos then error "Invalid input."
else (pascal (row-1) (pos-1)) + (pascal (row-1) (pos))
Error code:
Unexpected if expression in function application:
if row == 0 || pos == 0 then
"1"
else
if row > pos then
error "Invalid input."
else
(pascal (row - 1) (pos - 1)) + (pascal (row - 1) (pos))
You could write it with parentheses
Or perhaps you meant to enable BlockArguments?

pascal :: Int -> Int -> Int
pascal row pos =
if row == 0 || pos == 0 then 1
else if row > pos then error "Invalid input."
else (pascal (row-1) (pos-1)) + (pascal (row-1) (pos))
To get rid of that error you just need to add an =, which you probably just forgot. But this is really bad style in Haskell. This code begs for guards.

Related

Is it possible to create a string representation of a lambda in Kotlin?

I'm trying to get a toString or something similar of a lambda expression.
For example:
We have this expression
val isEvenThan1 = { x: Int ->
if (x % 2 == 0) 1
else 0
}
We wan a string output similar to this
"(x) -> { if (x % 2 == 0) 1 else 0 }"
I'm primarily looking for any libraries or work arounds to be able to output the internal logic of a function.

Switch case help in visual c++

The question is
int i = 6, x = 11;
switch(i % 3 ? 0 : 1)
{
case 0: x /=2; break;
case 1: x +=3;
case 2: x *=4;
}
cout << "x = " << x;
So the answer i should be getting is x=5, however, from the answer script it said it should be x= 56.
Why is that?
Both statements:
x +=3;
and
x *=4;
are executed. This is why (see here for conditional operator reference and here for the switch statement).
In the expression
i % 3 ? 0 : 1
i % 3 is the condition. Being an integer value, it's considered false if equal to zero, true otherwise. In your case i has value 6, thus i % 3 is 0, thus the condition is false and the value after the : is taken to be evaluated in the switch statement.
The value after the : is 1, so the code jumps to case 1 and executes x += 3. Now x is 11+3=14.
But there is no break statement before case 2, so the execution simply goes on with x *= 4 and x becomes 14*4=56.

issue with printing in haskell and function declaration

this is my code:
sumDig i = if (i == 0) then 0 else ((mod i 10) + somaDig ((i-(mod i 10)/10)))
main = do
print (sumDig 4)
it's not working and I dont know why... it doesn't compile and the compiler message is not helping
the function is just to sum all characters of a given number, eg: 123 = 1 + 2 + 3 = 6, and it does that using n mod 10 + recursion from n/10
You've got a few errors.
The error about ambiguous types can be fixed by adding a type annotation to sumDig
sumDig :: Int -> Int
Another error is that somaDig is undefined. Did you mean to type sumDig?
If so, then the last compile error is the use of / on integers. You probably want div instead:
sumDig :: Int -> Int
sumDig i = if (i == 0) then 0 else ((mod i 10) + sumDig (((i-(mod i 10)) `div` 10)))

Create number sequence of length x with only difference of 1 bit in successive numbers

Was asked this Amazon Telephonic Interview Round 1
So for Length = 1
0 1 (0 1)
Length = 2
00 01 11 10 (0, 1, 3, 2)
and so on
write function for length x that returns numbers in digit(base 10) form
That's called gray code, there are several different kinds, some of which are easier to construct than others. The wikipedia article shows a very simple way to convert from binary to gray code:
unsigned int binaryToGray(unsigned int num)
{
return (num >> 1) ^ num;
}
Using that, you only have to iterate over all numbers of a certain size, put them through that function, and print them however you want.
This is one way to do it:
int nval = (int)Math.Pow(2 , n);
int divisor = nval/2;
for (int i = 0; i < nval; i++)
{
int nb =(int) (i % divisor);
if ( nb== 2) Console.WriteLine(i + 1);
else if (nb == 3) Console.WriteLine(i - 1);
else Console.WriteLine(i);
}

Modified longest common substring

Given two strings what is an efficient algorithm to find the number and length of longest common sub-strings with the sub-strings being called common if :
1) they have at-least x% characters same and at same position.
2) the start and end indexes of the sub-strings being same.
Ex :
String 1 -> abedefkhj
String 2 -> kbfdfjhlo
suppose the x% being asked is 40,then, ans is,
5 1
where 5 is the longest length and 1 is the number of sub-strings in each string satisfying the given property. Sub-String is "abede" in string 1 and "kbfdf" in string 2.
You can use smth like Levenshtein distance without deleting and inserting.
Build the table, where every element [i, j] is error for substring from position [i] to position [j].
foo(string a, string b, int x):
len = min(a.length, b.length)
error[0][0] = 0 if a[0] == b[0] else 1;
for (end: [1 -> len-1]):
for (start: [end -> 0]):
if a[end] == b[end]:
error[start][end] = error[start][end - 1]
else:
error[start][end] = error[start][end - 1] + 1
best_len = 0;
best_pos = 0;
for (i: [0 -> len-1]):
for (j: [i -> 0]):
len = i - j + 1
error_percent = 100 * error[i][j] / len
if (error_percent <= x and len > best_len):
best_len = len
best_pos = j
return (best_len, best_pos)

Resources