Leap year function with only two conditions [closed] - leap-year

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
Let's have a function called isLeapYear, it's arguments are a year. It must return true if the year is leap and false if it is not.
The conditions, as wikipedia states, are:
if year is not divisible by 4 then common year
else if year is not divisible by 100 then leap year
else if year is divisible by 400 then leap year
else common year.
Can we define wether it is a leap year with just two conditions rather than three?

Yes, if you consider the following as "two conditions":
If a year is not divisible by 4, or divisible by 100 but not by 400, then it's a common year.
Otherwise, it's a leap year.
Implemented in C, for example:
int IsCommon(int year)
{
if (year%4 != 0 || (year%100 == 0 && year%400 != 0))
return 1;
else
return 0;
}
Please note however, that in terms of runtime-performance, there is not much to gain here by "condensing" Wikipedia's definition from "three conditions" into "two conditions". The logical operators (|| and &&) yield the same branching that a conditional statement such as if/else if would...
Here is an alternative solution, that relies on the way in which a switch/case statement is compiled, and performs only a single branching operation. Please note that it's a programmatic solution, not an algorithmic solution. It is suitable at least for C, C++ and Java (excluding minor semantics), but there are probably similar variations of it in other languages as well (for example, a dictionary in Python):
int IsCommon(int year)
{
switch (year%400)
{
case 0:
case 4:
case ...
case 96:
case 104:
case ...
case 196:
case 204:
case ...
case 296:
case 304:
case ...
case 396:
return 0;
}
return 1;
}
If my memory serves correctly, then a switch/case statement is compiled in the most efficient way when all cases are listed in an incremented and consecutive order, starting from 0. So you can further expand this code, as in the example below:
int IsCommon(int year)
{
switch (year%400)
{
case 0: return 0;
case 1: return 1;
case 2: return 1;
case 3: return 1;
case 4: return 0;
case 5: return 1;
case 6: return 1;
case 7: return 1;
case ...
case 96: return 0;
case 97: return 1;
case 98: return 1;
case 99: return 1;
case 100: return 1;
case 101: return 1;
case 102: return 1;
case 103: return 1;
case 104: return 0;
case 105: return 1;
case 106: return 1;
case 107: return 1;
case ...
case 196: return 0;
case 197: return 1;
case 198: return 1;
case 199: return 1;
case 200: return 1;
case 201: return 1;
case 202: return 1;
case 203: return 1;
case 204: return 0;
case 205: return 1;
case 206: return 1;
case 207: return 1;
case ...
case 296: return 0;
case 297: return 1;
case 298: return 1;
case 299: return 1;
case 300: return 1;
case 301: return 1;
case 302: return 1;
case 303: return 1;
case 304: return 0;
case 305: return 1;
case 306: return 1;
case 307: return 1;
case ...
case 396: return 0;
case 397: return 1;
case 398: return 1;
case 399: return 1;
}
return -1; // Just in order to prevent a compilation error (i.e., dummy)
}

No, there are three conditions and not two.

Related

Additional return statement while finding minimum depth of a Binary Search Tree

Following is the code that I found online to find the minimum depth of a binary search tree:
public class Solution {
public int minDepth(TreeNode root) {
if(root == null){
return 0;
}
LinkedList<TreeNode> nodes = new LinkedList<TreeNode>();
LinkedList<Integer> counts = new LinkedList<Integer>();
nodes.add(root);
counts.add(1);
while(!nodes.isEmpty()){
TreeNode curr = nodes.remove();
int count = counts.remove();
if(curr.left != null){
nodes.add(curr.left);
counts.add(count+1);
}
if(curr.right != null){
nodes.add(curr.right);
counts.add(count+1);
}
if(curr.left == null && curr.right == null){
return count;
}
}
return 0;
}
}
What I do not understand is the extra return statement at the end- return 0. Why is this needed?
It's for the case where the root isn't null, but it's the only node in the tree (the root is at depth 0). That return statement is needed because if the tree is empty, then something must be returned. It returns 0, because the depth is 0.
Similar to ghostofrasputin, the return statement is there because if the while condition is not met, then there is still a value to return.
Now the more important question, why do we need the last return if the program will never reach that return statement? (which I believe is this case for this)
Even though you are able to tell that the return statement wont be used, the compiler is not sophisticated enough to determine that, so it requires a return statement just in case the while loop is exited.
It's similar to the following code
public boolean getTrueOrFalse() {
if(Math.random() < 1) {
return true;
}
return false;
}
Though we know that this will always return true because Math.random() is always less than 1, the compiler isn't able to figure that out and thus the return statement is required if the if-statement is not met.

Issue with Recursive Methods ("missing return statement")

so I have a program that is running a bunch of different recursive methods, and I cannot get it to compile/run. The error is in this method, according to my computer:
public static int fibo(int n)
// returns the nth Fibonacci number
{
if (n==0)
{
return 0;
}
else if (n==1)
{
return 1;
}
else if (n>1)
{
return fibo(n-1) + fibo(n-2);
}
}
I have this method called correctly in my main method, so the issue is in this bit of code.
I think I can help you in this. Add return n; after your else if. Outside of the code but before the last curlicue.
The code will work as long as n ≥ 0 btw; another poster here is right in that you may want to add something to catch that error.
Make sure all possible paths have a return statement. In your code, if n < 0, there is no return statement, the compiler recognizes this, and throws the error.
public static int fibo(int n)
// returns the nth Fibonacci number
{
if (n<=0)
{
return 0;
}
else if (n==1)
{
return 1;
}
else // All other cases, i.e. n >= 1
{
return fibo(n-1) + fibo(n-2);
}
}

What will a break-statement do in Axapta?

I have 2 while loops, and in the second there is a break; (see the code below)
My question is: will the break cause a stop in the second loop or for the 2?
while select dirPartyRelationship
join dirPartyTable
where dirPartyTable.RecId == dirPartyRelationship.ChildParty
join dirPersonName
where dirPersonName.Person == dirPartyTable.RecId
{
while select checkDirRelationship
where checkDirRelationship.ChildParty == dirPartyRelationship.RecId
{
if (checkDirRelationship.RelationshipTypeId == _relationshipType)
{
break;
}
}...
The break will only break out of the current code block.
Create a job and use this sample code;
for(i=0; i<100; i++)
{
for(j=0; j<100; j++)
{
info(strfmt("inner loop count %1",j));
break;
}
info(strfmt("outer loop count %1",i));
}
You will see a quick example of j never getting over 0, but being printed 100 times.
Edit;
If you want to break out of the nested loop you could work around by declaring a boolean, maybe called breakAll, and setting breakAll to true before the break; line in the inner loop. In the outer loop check for breakAll like this;
for(i=0; i<100; i++)
{
for(j=0; j<100; j++)
{
info(strfmt("inner loop count %1",j));
if (somethingToCheck)
{
breakAll = true;
break;
}
}
info(strfmt("outer loop count %1",i));
if (breakAll)
{
break;
}
}

Ternary usage in FizzBuzz++ 1.5 (codeacademy)

I've been running through codeacademy's tutorials and projects. On FizzBuzz++ 1.5 they want us to re-write the "Wobble" function as Wob, using ternary operators. I keep getting an error saying "missing operand" with the following code. Also the +1 on the end of the return, how does that work, does javaScript store it as a temp value because it doesn't get assigned to any var. Thanks for the help. Code is below.
var Wibble = {
Wobble: function(a, b) {
if(a===b)
//if the variables are equal return 0
return 0;
else {
//decrement the larger of the 2 values
if (a>b) {
a--;
} else {
b--;
}
//call this function again with the decremented values
//once they are equal the functions will return up the stack
//adding 1 to the return value for each recursion
return Wibble.Wobble(a,b)+1;
}
},
//This is the line of code that doesn't want to function..
Wob: function(a, b) {
(a===b) ? return 0 :(a<b) ? return this.Wob(a--,b)+1 : return this.Wob(a,b--)+1;
}
};
The following expression with the ternary operator:
result = (a) ? x : y;
is equivalent to the following:
if(a)
{
result = x;
}
else
{
result = y;
}
Note the syntactical difference, where in the ternary operator you are switching in the assignment, whereas in the if statement syntax, you are assigning in the switch.
That is to say that:
(a == b) ? return 0 : return 1;
is not equivalent to:
if(a == b)
return 0;
else
return 1;
Instead, you would want to write:
return (a == b) ? 0 : 1;

Using recursion to sum numbers

I have just been studying the concept of recursion and I thought that I would try a simple example. In the following code, I am attempting to take the numbers: 1, 2, 3, 4, 5, and add them together using recursion. I expected the result to be 15, but my code is returning 16.
What am I doing wrong?
Code:
static void Main(string[] args)
{
Console.WriteLine(Sum(5));
Console.Read();
}
static int Sum(int value)
{
if (value > 0)
{
return value + Sum(value - 1);
}
else
{
return 1;
}
}
You're returning 1 in the else clause. You should be returning 0:
else
{
return 0;
}
If the value is not greater than zero, why would you return one in the first place?
Your code executes as follows:
Sum --> 5
Sum --> 4
Sum --> 3
Sum --> 2
Sum --> 1
Sum --> 0
1 <---
2 <---
4 <---
7 <---
11 <---
16 <---
Check your base case.
Others already noted the error, and I will elaborate on recursion.
Although C# does not currently perform tail call optimization (although IL has special tail instruction), it's worth mentioning that tail recursion is generally a good thing.
Tail recursion is a special case of recursion in which the last operation of the function, the tail call, is a recursive call. Since the last call is the recursive call there is no need to preserve stack frame of the calling function and the compiler can easily use this information to generate machine instruction such that the stack doesn't grow at all. So it can basically turn recursive function into an iterative one.
Rewriting your code to support tail recursion can be done as follws:
static int Sum(int result, int value)
{
if(value == 0)
return result;
return Sum(result + 1, value - 1);
}
static int Sum(int value)
{
if (value > 0)
{
return value + Sum(value - 1);
}
else
{
return 0; //Change this.
}
}
That's because, when the value is = 0, you return 1. Then it get's added.
Sum's "else" clause should return 0.
I always prefer to put the terminating case(s) up front so they're obvious, and I have a violent near-psychopathic hatred of "if cond then return a else return b" constructs. My choice would be (making it clear that it won't work properly for negative numbers):
static unsigned int Sum(unsigned int value) {
if (value == 0)
return 0;
return value + Sum(value - 1);
}
I believe that's far more readable than a morass of braces and control flow.
The others have already answered that question, but when I work with recursion, one of the things I like to do to check that it works is to use check the base case and one additional case. I your case I would test it with 1, which would yield 2. Since this is obviously wrong you might want to check for 0 which is not going to use any recursion and so it should be obvious that the error lies in the base class.
In general recursion is easier to reason about, since you can list the limited number of things you need to check, but it does initially require a leap of faith since your intuition will be wrong. Just test the edge cases and trust the math it will never fail.
int summation(int num){
if (num==1)
return 1;
return summation(num-1)+num;
}
I'm pretty sure the problem is because you want your recursion to terminate when value == 1, and it's currently terminating when value == 0.
Your terminating expression is at issue. When value == 0 (or lower), it should return a 0 rather than 1. For sake of efficiency (which, let's admit it here, obviously isn't a concern, otherwise recursion wouldn't have been used for this task), you should terminate the recursion at value == 1 and return a literal 1 to save one unnecessary level of recursion.
using System;
using NUnit.Framework;
namespace Recursion
{
[TestFixture()]
public class Test
{
[Test()]
public void TestSum ()
{
Assert.AreEqual (Sum (new int[] { }), 0);
Assert.AreEqual (Sum (new int[] { 0 }), 0);
Assert.AreEqual (Sum (new int[] { 1 }), 1);
Assert.AreEqual (Sum (new int[] { 1, 2, 3, 4, 5 }), 15);
}
public int Sum(int[] head)
{
if (head.Length == 0) return 0;
int[] tail = new int[head.Length - 1];
for (int i = 1; i < head.Length; i++)
{
tail [i-1] = head [i];
}
return head[0] + Sum (tail);
}
}
}
It could also be written like this:
public static int sum(int n){
int total;
if(n==1){
total =1;
}else{
total = sum(n-1)+n;
}
return total;
}
Actually, I think you don't need to check case else because
public static int sum(int number){
if(number > 0){
return number + sum(--number);
}
return number; // return 0 so that's why you don't need check else condition
}
To begin at the end, a recursive Sum method looks like this:
// version 3
public static int Sum(int startRange, int endRange)
{
if (endRange > startRange)
{
return endRange + Sum(startRange, endRange - 1);
}
if (endRange < startRange)
{
return startRange + Sum(endRange, startRange - 1);
}
return endRange;
}
Hardcoding the startRange to be 0 gives us:
// version 2
public static int Sum(int range)
{
if (range > 0)
{
return range + Sum(0, range - 1);
}
if (range < 0)
{
return Sum(range, -1);
}
return range;
}
...and if you want to limit the method to positive numbers only, there's no need for a sign:
// version 1
public static unsigned int Sum(unsigned int range)
{
if (range > 0)
{
return range + Sum(0, range - 1);
}
return range;
}
I hope this helps give more of an insight into summing number ranges via recursion.
static int Sum(int[] addends)
{
if (addends.Length == 1)
{
return addends[0];
}
else
{
int tailIndex = addends.Length - 1;
var subArray = addends[0..tailIndex];
return addends[tailIndex] + Sum(subArray);
}
}
Try this code:
def sumr(n):
if n==0:
return n
return n+sumr(n-1)

Resources