0% found this document useful (0 votes)
53 views

Logic and Logic-2

The document describes functions that take integers as parameters and return booleans or integers based on certain logic rules. The functions cover topics like checking ranges, sums, remainders, and more. Twenty-seven test cases are provided for each function.

Uploaded by

Muni Prakash
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Logic and Logic-2

The document describes functions that take integers as parameters and return booleans or integers based on certain logic rules. The functions cover topics like checking ranges, sums, remainders, and more. Twenty-seven test cases are provided for each function.

Uploaded by

Muni Prakash
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.When squirrels get together for a party, they like to have cigars.

A squirrel
party is successful when the number of cigars is between
40 and 60, inclusive. Unless it is the weekend, in which case there is no upper
bound on the number of cigars. Return true if the party
with the given values is successful, or false otherwise.

cigarParty(30, false) → false


cigarParty(50, false) → true
cigarParty(70, true) → true

2.You and your date are trying to get a table at a restaurant. The parameter "you"
is the stylishness of your clothes, in the range 0..10,
and "date" is the stylishness of your date's clothes. The result getting the table
is encoded as an int value with 0=no, 1=maybe, 2=yes.
If either of you is very stylish, 8 or more, then the result is 2 (yes). With the
exception that if either of you has style of 2 or less,
then the result is 0 (no). Otherwise the result is 1 (maybe).

dateFashion(5, 10) → 2
dateFashion(5, 2) → 0
dateFashion(5, 5) → 1

3.The squirrels in Palo Alto spend most of the day playing. In particular, they
play if the temperature is between 60 and 90 (inclusive).
Unless it is summer, then the upper limit is 100 instead of 90. Given an int
temperature and a boolean isSummer, return true if the
squirrels play and false otherwise.

squirrelPlay(70, false) → true


squirrelPlay(95, false) → false
squirrelPlay(95, true) → true

4.You are driving a little too fast, and a police officer stops you. Write code to
compute the result, encoded as an int value: 0=no ticket
,1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is
between 61 and 80 inclusive, the result is 1. If speed
is 81 or more, the result is 2. Unless it is your birthday -- on that day, your
speed can be 5 higher in all cases.

caughtSpeeding(60, false) → 0
caughtSpeeding(65, false) → 1
caughtSpeeding(65, true) → 0

5.Given 2 ints, a and b, return their sum. However, sums in the range 10..19
inclusive, are forbidden, so in that case just return 20.

sortaSum(3, 4) → 7
sortaSum(9, 4) → 20
sortaSum(10, 11) → 21

6.Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean
indicating if we are on vacation, return a string of the
form "7:00" indicating when the alarm clock should ring. Weekdays, the alarm should
be "7:00" and on the weekend it should be "10:00".
Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it
should be "off".
alarmClock(1, false) → "7:00"
alarmClock(5, false) → "7:00"
alarmClock(0, false) → "10:00"

7.The number 6 is a truly great number. Given two int values, a and b, return true
if either one is 6. Or if their sum or difference is 6.
Note: the function Math.abs(num) computes the absolute value of a number.

love6(6, 4) → true
love6(4, 5) → false
love6(1, 5) → true

8.Given a number n, return true if n is in the range 1..10, inclusive. Unless


outsideMode is true, in which case return true if the number
is less or equal to 1, or greater or equal to 10.

in1To10(5, false) → true


in1To10(11, false) → false
in1To10(11, true) → true

9.
We'll say a number is special if it is a multiple of 11 or if it is one more than a
multiple of 11. Return true if the given non-negative
number is special. Use the % "mod" operator -- see Introduction to Mod

specialEleven(22) → true
specialEleven(23) → true
specialEleven(24) → false

10.Return true if the given non-negative number is 1 or 2 more than a multiple of


20. See also: Introduction to Mod

more20(20) → false
more20(21) → true
more20(22) → true

11.Return true if the given non-negative number is a multiple of 3 or 5, but not


both. Use the % "mod" operator -- see Introduction to Mod

old35(3) → true
old35(10) → true
old35(15) → false

12.Return true if the given non-negative number is 1 or 2 less than a multiple of


20. So for example 38 and 39 return true, but 40 returns
false. See also: Introduction to Mod

less20(18) → true
less20(19) → true
less20(20) → false
13.Given a non-negative number "num", return true if num is within 2 of a multiple
of 10. Note: (a % b) is the remainder of dividing a by
b, so (7 % 5) is 2. See also: Introduction to Mod

nearTen(12) → true
nearTen(17) → false
nearTen(19) → true

14.Given 2 ints, a and b, return their sum. However, "teen" values in the range
13..19 inclusive, are extra lucky. So if either value is a
teen, just return 19.

teenSum(3, 4) → 7
teenSum(10, 13) → 19
teenSum(13, 2) → 19

15.Your cell phone rings. Return true if you should answer it. Normally you answer,
except in the morning you only answer if it is your
mom calling. In all cases, if you are asleep, you do not answer.

answerCell(false, false, false) → true


answerCell(false, false, true) → false
answerCell(true, false, false) → false

16.We are having a party with amounts of tea and candy. Return the int outcome of
the party encoded as 0=bad, 1=good, or 2=great. A party
is good (1) if both tea and candy are at least 5. However, if either tea or candy
is at least double the amount of the other one, the party
is great (2). However, in all cases, if either tea or candy is less than 5, the
party is always bad (0).

teaParty(6, 8) → 1
teaParty(3, 8) → 0
teaParty(20, 6) → 2

17.Given three ints, a b c, return true if it is possible to add two of the ints to
get the third.

twoAsOne(1, 2, 3) → true
twoAsOne(3, 1, 2) → true
twoAsOne(3, 2, 2) → false

18.Given three ints, a b c, return true if b is greater than a, and c is greater


than b. However, with the exception that if "bOk" is true,
b does not need to be greater than a.

inOrder(1, 2, 4, false) → true


inOrder(1, 2, 1, false) → false
inOrder(1, 1, 2, true) → true

19.Given three ints, a b c, return true if they are in strict increasing order,
such as 2 5 11, or 5 6 7, but not 6 5 7 or 5 5 7. However,
with the exception that if "equalOk" is true, equality is allowed, such as 5 5 7 or
5 5 5.
inOrderEqual(2, 5, 11, false) → true
inOrderEqual(5, 7, 6, false) → false
inOrderEqual(5, 5, 7, true) → true

20.Given three ints, a b c, return true if two or more of them have the same
rightmost digit. The ints are non-negative. Note: the % "mod"
operator computes the remainder, e.g. 17 % 10 is 7.

lastDigit(23, 19, 13) → true


lastDigit(23, 19, 12) → false
lastDigit(23, 19, 3) → true

21.Given three ints, a b c, return true if one of them is 10 or more less than one
of the others.

lessBy10(1, 7, 11) → true


lessBy10(1, 7, 10) → false
lessBy10(11, 1, 7) → true

22.Return the sum of two 6-sided dice rolls, each in the range 1..6. However, if
noDoubles is true, if the two dice show the same value,
increment one die to the next value, wrapping around to 1 if its value was 6.

withoutDoubles(2, 3, true) → 5
withoutDoubles(3, 3, true) → 7
withoutDoubles(3, 3, false) → 6

23.Given two int values, return whichever value is larger. However if the two
values have the same remainder when divided by 5, then the
return the smaller value. However, in all cases, if the two values are the same,
return 0. Note: the % "mod" operator computes the
remainder, e.g. 7 % 5 is 2.

maxMod5(2, 3) → 3
maxMod5(6, 2) → 6
maxMod5(3, 2) → 3

24.You have a red lottery ticket showing ints a, b, and c, each of which is 0, 1,
or 2. If they are all the value 2, the result is 10.
Otherwise if they are all the same, the result is 5. Otherwise so long as both b
and c are different from a, the result is 1. Otherwise
the result is 0.

redTicket(2, 2, 2) → 10
redTicket(2, 2, 1) → 0
redTicket(0, 0, 0) → 5

25.You have a green lottery ticket, with ints a, b, and c on it. If the numbers are
all different from each other, the result is 0.
If all of the numbers are the same, the result is 20. If two of the numbers are the
same, the result is 10.
greenTicket(1, 2, 3) → 0
greenTicket(2, 2, 2) → 20
greenTicket(1, 1, 2) → 10

26.You have a blue lottery ticket, with ints a, b, and c on it. This makes three
pairs, which we'll call ab, bc, and ac. Consider the sum
of the numbers in each pair. If any pair sums to exactly 10, the result is 10.
Otherwise if the ab sum is exactly 10 more than either bc
or ac sums, the result is 5. Otherwise the result is 0.

blueTicket(9, 1, 0) → 10
blueTicket(9, 2, 0) → 0
blueTicket(6, 1, 4) → 10

27.Given two ints, each in the range 10..99, return true if there is a digit that
appears in both numbers, such as the 2 in 12 and 23.
(Note: division, e.g. n/10, gives the left digit while the % "mod" n%10 gives the
right digit.)

shareDigit(12, 23) → true


shareDigit(12, 43) → false
shareDigit(12, 44) → false

LOGICS-2

1.We want to make a row of bricks that is goal inches long. We have a number of
small bricks (1 inch each) and big bricks (5 inches each).
Return true if it is possible to make the goal by choosing from the given bricks.
This is a little harder than it looks and can be done
without any loops. See also: Introduction to MakeBricks

makeBricks(3, 1, 8) → true
makeBricks(3, 1, 9) → false
makeBricks(3, 2, 10) → true

2.Given 3 int values, a b c, return their sum. However, if one of the values is the
same as another of the values, it does not count towards
the sum.

loneSum(1, 2, 3) → 6
loneSum(3, 2, 3) → 2
loneSum(3, 3, 3) → 0

3.Given 3 int values, a b c, return their sum. However, if one of the values is 13
then it does not count towards the sum and values to its
right do not count. So for example, if b is 13, then both b and c do not count.

luckySum(1, 2, 3) → 6
luckySum(1, 2, 13) → 3
luckySum(1, 13, 3) → 1

4.Given 3 int values, a b c, return their sum. However, if any of the values is a
teen -- in the range 13..19 inclusive -- then that value
counts as 0, except 15 and 16 do not count as a teens. Write a separate helper
"public int fixTeen(int n) {"that takes in an int value and
returns that value fixed for the teen rule. In this way, you avoid repeating the
teen code 3 times (i.e. "decomposition"). Define the
helper below and at the same indent level as the main noTeenSum().

noTeenSum(1, 2, 3) → 6
noTeenSum(2, 13, 1) → 3
noTeenSum(2, 1, 14) → 3

5.For this problem, we'll round an int value up to the next multiple of 10 if its
rightmost digit is 5 or more, so 15 rounds up to 20.
Alternately, round down to the previous multiple of 10 if its rightmost digit is
less than 5, so 12 rounds down to 10. Given 3 ints, a b c,
return the sum of their rounded values. To avoid code repetition, write a separate
helper "public int round10(int num) {" and call it 3
times. Write the helper entirely below and at the same indent level as roundSum().

roundSum(16, 17, 18) → 60


roundSum(12, 13, 14) → 30
roundSum(6, 4, 4) → 10

6.Given three ints, a b c, return true if one of b or c is "close" (differing from


a by at most 1), while the other is "far", differing
from both other values by 2 or more. Note: Math.abs(num) computes the absolute
value of a number.

closeFar(1, 2, 10) → true


closeFar(1, 2, 3) → false
closeFar(4, 1, 3) → true

7.Given 2 int values greater than 0, return whichever value is nearest to 21


without going over. Return 0 if they both go over.

blackjack(19, 21) → 21
blackjack(21, 19) → 21
blackjack(19, 22) → 19

8.Given three ints, a b c, one of them is small, one is medium and one is large.
Return true if the three values are evenly spaced, so the
difference between small and medium is the same as the difference between medium
and large.

evenlySpaced(2, 4, 6) → true
evenlySpaced(4, 6, 2) → true
evenlySpaced(4, 6, 3) → false

9.We want make a package of goal kilos of chocolate. We have small bars (1 kilo
each) and big bars (5 kilos each). Return the number of
small bars to use, assuming we always use big bars before small bars. Return -1 if
it can't be done.

makeChocolate(4, 1, 9) → 4
makeChocolate(4, 1, 10) → -1
makeChocolate(4, 1, 7) → 2

You might also like