-4

I am in codewars and the challenge I have has one part I can't get my head around this is what I am speaking of

You are given a number "n" (n >= 0) and a digit "d" (0 <= d <= 9).

Write a function nbDig(n, d) that finds the square of each integer from 0 to n, and returns the number of times that the digit d appears across all the squares. Note that d might appear multiple times in a single square.

Example:

n=12, d=1 Squares from 0 to n=12: 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144 The function returns 7 because the digit d=1 appears 7 times: in 1, 16, 81, 100, 121 (note: 1 appears twice in 121), and 144.

the part the I have trouble with is how do I get my function to get the square roots of everything between 0 and n and when the numbers I have to solve are large numbers like (5750)(195856) or (74747)

Logono
  • 5
  • 1
  • 2
    Can you show us where do you get stuck? – Daniel Hao Jun 16 '22 at 00:43
  • 3
    Code challenges are... a challenge. I assume you're learning Python and trying to get better at it. Unfortunately, as you can see below, answers to questions asked in this manner tend to just be solutions to the challenge, which don't really help you learn to do it on your own. If you craft your question to be more specific — show what you've tried, and point out specifically where it isn't working correctly, or which part you don't understand — you're more like to get answers that actually explain the things you're trying to figure out. – CrazyChucky Jun 16 '22 at 00:51
  • 2
    And more generally, welcome to Stack Overflow! To help you get the most out of your time here, I recommend taking the [tour] and reading [ask]. You may find some of the advice in [How to ask and answer homework questions](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) useful as well; this isn't schoolwork, but the same ideas apply. – CrazyChucky Jun 16 '22 at 00:53
  • 1
    Can you please provide the link to the challenge? Sounds fun... – ddejohn Jun 16 '22 at 01:07
  • Where are the "extremely large" numbers? I only see small ones. – Kelly Bundy Jun 16 '22 at 03:26
  • @ddejohn - [challenge link](https://www.codewars.com/kata/566fc12495810954b1000030) – DarrylG Jun 16 '22 at 06:58

1 Answers1

-2

If I am interpreting your question correctly, you simply need to devise a very simple list comprehension program.

Getting the square root of everything between zero and n is not necessary. Did you mean square?

Here's my solution:

def nbDig(n,d):
    int_list = str([str(x*x) for x in range(0,n+1)]).count(str(d))
    return int_list

I can test it like so:

nbDig(12,1)

This returns 7.

EDIT: I didn't realize calling .replace was totally unnecessary. Thanks for the feedback!

433MEA
  • 100
  • 8
  • https://www.codewars.com/kata/566fc12495810954b1000030 this is the link – Logono Jun 16 '22 at 01:18
  • 2
    Not sure what happened to my comment, but basically, there's a lot of unnecessary casting to `str` and repeated calls to `.replace`, etc. Possibly also because it's generally frowned upon on Stack Overflow to answer "challenge" questions like this (see CrazyChucky's comments). – ddejohn Jun 16 '22 at 01:38
  • 1
    Simplifying your solution to `str([x * x for x in range(n + 1)]).count(str(d))` halves the time it takes for moderately large `n` like `1_000_000`. – ddejohn Jun 16 '22 at 01:48