Make a recursive ruby function [closed] - ruby

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Make a RECURSIVE ruby function "double_fact(n)" defined as follows –
n!! = 1 if n = −1 or n = 0 or n = 1;
n(n − 2)!! otherwise.
Outputs the result of double_fact() respecting to a value specified
from the command line.
//Hint: Ruby has the usual "and", "or" and "not" operators. You may
need "or" to test multiple conditions here. Also, doublefact(8) = 384.

The problem statement is very misleading. You don't need any boolean operators at all, you can just translate the mathematical definition 1:1 into Ruby:
def doublefact(n)
return 1 if (-1..1).include?(n)
n * doublefact(n-2)
end

Related

Solving Inequalities In Programming [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
We are given a variable that have some constraints over its range of value, we have to overall find out a set that denotes it overall range.
For example and conditions are as follows
x< 10
x> -6
x>= 0
I can do it on real number line and mark the intersection but how to do it logically in programming.
Note : Only > , >= , < , <= are allowed.
ANSWER=[0, 10)
You have to figure out the logic of your solution, then implement that logic in C++. You say you "can do it", which I assume means you find it "easy" to solve as a humain being. What makes it so easy? Identify the method you're using, then write that method in C++.
There are two types of inequalities: > and <. Well, there are also <= and >=, but I suggest leaving those aside until you've written a program that handles < and > correctly.
Imagine you have:
x > 5
x > 7
x > 6
x < 11
x < 10
x < 12.
What is the solution in this case? Try to find the solution without drawing the number line. Then try to describe with words the way you arrived to this solution.
Then try to write pseudo-code that describes the algorithm more formally.
Finally, you're ready to write C++ code that performs the same steps. I suggest not trying to write C++ until you have written pseudo-code. When writing C++ you'll encounter a few cumbersome details; for instance, how to parse each expression, such as x < 5, to find out what inequality it is and which number it's comparing x to. These "details" are not uninteresting but they will get in the way of your logic so it's best to keep them for last.

Create a List of string in ascending Order in lexicographically order [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to generate a algorithm in which I want to get the next string in lexicographically order.
Suppose I want to generate a list of length 26 then it is
['a','b'....'z']
Now suppose I want to generate a list of length 260 then it is
['a0','a1','a2'...'a9','b1'....'z0'....'z9']
This type of algorithm have max-limit. But I don't want such type of limitations. It may be 10000 or 1 millions.
Requirement
Algorithm should work in such a way that previously string is passed as argument generate by it. And it should produce the next string in lexicographically order. And I do not want to use timestamp (1503314045645)
Thanks
What about using base 36 formatted integers?
It looks like this in java:
String next(String prev) {
if(prev==null) {
return "0";
}
return Integer.toString(Integer.parseInt(prev, 36), 36);
}
Actually it's even better if you you use a simple integer for storing the value, and simply increase it every time you need the next value and format the integer using base 36 into a string:
Integer.toString(++value, 36);
In this solution the numbers are before the letters in the output, so you will get the following tokens:
a7,a8,a9,aa,ab, ... ax,ay,az,b0,b1 ... zx,zy,zz,100,101
If you want letters first or want any specific order or extra characters, then use the solution behind Matt Timmermans' link.

Stack implementation the Trollface way [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In my software engineering course, I encountered the following characteristic of a stack, condensed by me: What you push is what you pop. The fully axiomatic version I Uled here.
Being a natural-born troll, I immediately invented the Troll Stack. If it has more than 1 element already on it, pushing results in a random permutation of those elements. Promptly I got into an argument with the lecturers whether this nonsense implementation actually violates the axioms. I said no, the top element stays where it is. They said yes, somehow you can recursively apply the push-pop-axiom to get "deeper". Which I don't see. Who is right?
The violated axiom is pop(push(s,x)) = s. Take a stack s with n > 1 distinct entries. If you implement push such that push(s,x) is s'x with s' being a random permutation of s, then since pop is a function, you have a problem: how do you reverse random_permutation() such that pop(push(s,x)) = s? The preimage of s' might have been any of the n! > 1 permutations of s, and no matter which one you map to, there are n! - 1 > 0 other original permutations s'' for which pop(push(s'',x)) != s''.
In cases like this, which might be very easy to see for everybody but not for you (hence your usage of the "troll" word), it always helps to simply run the "program" on a piece of paper.
Write down what happens when you push and pop a few times, and you will see.
You should also be able to see how those axioms correspond very closely to the actual behaviour of your stack; they are not just there for fun, but they deeply (in multiple meanings of the word) specify the data structure with its methods. You could even view them as a "formal system" describing the ins and outs of stacks.
Note that it is still good for you to be sceptic; this leads to a) better insight and b) detection of errors your superiours make. In this case they are right, but there are cases where it can save you a lot of time (e.g. while searching the solution for the "MU" riddle in "Gödel, Escher, Bach", which would be an excellent read for you, I think).

Halting program explained [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to understand why is it impossible to write a program H that can check whether another program P on a specific input I will halt or not (Halting problem), but i am unable to get a clear idea of it.
Though intuition says that if this program H tries to run a program P which is not going to halt, then H itself will go into a loop. But i dont think this is the idea behind the proof.
Can anybody explain the proof in simple layman terms?
The idea behind the proof is by contradiction.
Assume there is a Halting Problem Machine M that solves the Halting problem, and yields 0 if some input program won't finish, and 1 if it will. M is guaranteed to finish.
Create a new machine H.
H runs M with the input of H (itself), and if M answers 1 - get into endless loop, otherwise - stop.
Now, what will happen if you run M on input H?
If the answer is 1 - than it is wrong, since H runs M, and will get to infinite loop.
If the answer is 0 - it is also wrong, since H will stop.
So, it is contradicting the assumption that M is correct - and thus there is no such M.
Intuitively - it is like saying there is no such thing as an oracle, because if "you" tell me you're an oracle, I ask you - which arm am I going to raise?
Then, I will wait for your answer - and do the opposite - which contradicts the claim that the oracle is indeed an oracle.
Turing used proof by contradiction for this (aka reduction to absurdity):
The idea is to assume there is actually such a machine H that given any program p and an input i will tell us weather p stops.
Given such H, we can modify it and create a new machine.
We'll add another part after H's output such that if H outputs yes, our machine will loop infinitely,
and if H outputs no, our new machine will halt.
We'll call our new machine H+.
Now, the paradox occurs when we feed H+ to itself (as both program p and input i).
That's because if H+ does halt, we get a yes answer (from the H part), but then it loops forever.
However, if H+ doesn't halt, we get a no answer (from the H part), but then it halts.
This is explained very nicely in this computerphile episode.

How to read dale-chall mathematical notation? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How do I calculate this dale-chall mathematical notation? or how is it converted to easier pseudo code?
I am trying to understand this concept to implement a text readability analyzer.
Is it like the following? altough ... how is what comes after 0.1579 and 0.0496 calculated?
0.1579 ( (difficult words - words) * 100 ) + 0.0496 (words - sentences)
The given formula will be written as follows in the most common programming languages:
(0.1579 * ((difficultWords / words) * 100)) + (0.0496 * (words / sentences))
The above expression will work in Python, Ruby, Javascript, Java, C, C++, C#, etc. Notice that we use * for multiplication (you can't omit the operator) and / for division, and we add as many parentheses as needed to eliminate any ambiguities in evaluation order.
When you're actually implementing the above code you'll have to be careful with divisions - some languages (for example: Java, Python 2.x) will truncate decimals if both operands are integer values. To get around this problem you can either declare the variables difficultWords, words and sentences using a data type that allows for decimals (say, double) or you can explicitly convert the variables to a decimal data type at the time of performing the division. For example, the formula will look like this in Java:
(0.1579 * (((double) difficultWords / words) * 100)) + (0.0496 * ((double) words / sentences))

Resources