Solved

count7 challenge

Posted on 2016-09-09
12
104 Views
Last Modified: 2016-10-11
Hi,

I am working on below challenge

http://codingbat.com/prob/p101409


Given a non-negative int n, return the count of the occurrences of 7 as a digit, so for example 717 yields 2. (no loops). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).

count7(717) → 2
count7(7) → 1
count7(123) → 0

Not clear on how to get 7 count recursively. please advisde
0
Comment
Question by:gudii9
  • 6
  • 5
12 Comments
 
LVL 25

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 250 total points
ID: 41792254
Enjoy :)

--

DECLARE @intValue AS VARCHAR(10) = '1717E7'
 
SELECT SUM( CASE WHEN SUBSTRING(@intValue,number,1) LIKE '[7]' THEN 1 ELSE 0 END ) Count7DIGITS FROM
( 
  SELECT DISTINCT number FROM
  MASTER..SPT_VALUES WHERE number > 0 AND number <= DATALENGTH(@intValue) 
) x

--

Open in new window

0
 
LVL 27

Accepted Solution

by:
rrz earned 250 total points
ID: 41792664
CodingBat gave you the operations you need to use.
Since the challenge asks us to return a count, we can have the "base case" return 0 and not affect the count.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41793654
public int count7(int n) {
  if (n < 1) {
    return 0;
  }
  if ((n % 10) == 7){ return 1 + count7(n/10);
  }
  else {
  return count7(n/10);
  }
}

Open in new window


above pass all. any improvement and alternate approaches?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41793727
That is good. My solution is the same as yours.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41796948
if then if then else
in above fashion code is there else belong to top if or second(last if in this case)?

public int count7(int n) {
  if (n < 1) {//else belongs to this if ??
    return 0;
  }
  if ((n % 10) == 7){ return 1 + count7(n/10);//below else belongs to this if or above if?
  }
 [b] else {
  return count7(n/10);
  }[/b]

Open in new window

}
0
 
LVL 27

Expert Comment

by:rrz
ID: 41797003
The else block is for the second if
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 7

Author Comment

by:gudii9
ID: 41798174
The else block is for the second if
how we know. is that is rule always else belong to its immediate top if not topmost if?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41798326
how we know. is that is rule
Please read  
https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.9   
It is called
The if-then-else Statement.  
The "else" is always associated with the "if" that proceeds it.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41798335
oh
https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.9
as per above link
IfThenStatement:
if ( Expression ) Statement
IfThenElseStatement:
if ( Expression ) StatementNoShortIf else Statement
IfThenElseStatementNoShortIf:
if ( Expression ) StatementNoShortIf else StatementNoShortIf
what is StatementNoShortIf
is there something like shortif and lognif?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41798424
what is StatementNoShortIf
That is confusing.  I can't really explain why they wrote that. But, it is linked to
https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.5
0
 
LVL 7

Author Comment

by:gudii9
ID: 41798488
The "else" is always associated with the "if" that proceeds it.

in that link where they mentioned above point?

14.9.2. The if-then-else Statement

An if-then-else statement is executed by first evaluating the Expression. If the result is of type Boolean, it is subject to unboxing conversion (§5.1.8).

If evaluation of the Expression or the subsequent unboxing conversion (if any) completes abruptly for some reason, then the if-then-else statement completes abruptly for the same reason.

Otherwise, execution continues by making a choice based on the resulting value:

If the value is true, then the first contained Statement (the one before the else keyword) is executed; the if-then-else statement completes normally if and only if execution of that statement completes normally.

If the value is false, then the second contained Statement (the one after the else keyword) is executed; the if-then-else statement completes normally if and only if execution of that statement completes normally.

14.10. The assert Statement

That is confusing.
not sure either on  what they meant by unboxing in above context?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41798613
not sure either on  what they meant by unboxing in above context?
They reference (§5.1.8). Which is at  
https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.8 
or maybe it is better explained at  
https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now