codingbat | Solutions to CodingBat problems | Learning library
kandi X-RAY | codingbat Summary
kandi X-RAY | codingbat Summary
Solutions to CodingBat problems.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of codingbat
codingbat Key Features
codingbat Examples and Code Snippets
Community Discussions
Trending Discussions on codingbat
QUESTION
I'm trying to solve this problem here: https://codingbat.com/prob/p252079?parent=/home/peter@norvig.com
In math, a "combination" of a set of things is a subset of the things. We define the function combinations(things, k) to be a list of all the subsets of exactly k elements of things. Conceptually, that's all there is, but there are some questions to settle: (A) how do we represent a subset? (B) What order are the elements within each subset? (C) What order to we list the subsets? Here's what we will agree to: (A) a subset will be a list. (B) The order of elements within a list will be the same as the order within 'things'. So, for example, for combinations([1, 2, 3], 2) one of the subsets will be [1, 2]; whereas [2, 1] is not a subset. (C) The order of subsets will be lexicographical or sorted order -- that is, combinations([1, 2, 3], 2) returns [ [1, 2], [1, 3], 2, 3] ] because [1, 2] < [1, 3] < [2, 3]. You might want to use the function 'sorted' to make sure the results you return are properly ordered.
combinations([1, 2, 3, 4, 5], 2) → [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]]
combinations([1, 2, 3], 2) → [[1, 2], [1, 3], [2, 3]]
combinations([1, 2, 3, 4, 5, 6], 5) → [[1, 2, 3, 4, 5], [1, 2, 3, 4, 6], [1, 2, 3, 5, 6], [1, 2, 4, 5, 6], [1, 3, 4, 5, 6], [2, 3, 4, 5, 6]]
Here's my code:
...ANSWER
Answered 2022-Apr-08 at 05:43The problem is this. When k == 0
it shouldn't return [things]
. It should return an empty array. Similar to when len(things) < k:
. This is because, when k == 0
, it means we that we have already found all the numbers for that specific combination.
But there's one more problem. We're returning an empty array. However, in the for
loops, we're iterating over the returned array. So if the array is empty, nothing happens. So what we should really return is an empty 2D array. I won't go into too much detail about what the problem is since it's better for you to try and understand why it's not working. Try adding print statements inside and outside the for loops.
Anyway, the working code looks like this:
QUESTION
I am practicing on CodingBat and trying the below question:
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.
Test cases:
make_bricks(3, 1, 8)
→True
make_bricks(3, 1, 9)
→False
make_bricks(3, 2, 10)
→True
make_bricks(7, 1, 13)
→False
make_bricks(1, 4, 12)
→False
When I run on my code on code editor(VSCode), I pass every test cases but when I do submit on the CodingBat(https://codingbat.com/prob/p118406) I am getting and error as Time out. Please can anyone explain me why or is there any error in my code below:
...ANSWER
Answered 2022-Mar-31 at 06:30You can calculate this without loops. It's the loops that are taking too much time. Some simple arithmetic and a couple of quick early checks should solve this issue:
QUESTION
This is my first time posting to Stackoverflow.
I'm trying to solve this problem here: https://codingbat.com/prob/p270692?parent=/home/konstans@stuy.edu/all
When looking at all hailstone sequences from 1 to z, maxHail(z) will return the starting number that creates the longest sequence. In other words, maxHail(n) looks at hailLen(1), hailLen(2) ... hailLen(n) and returns the number from 1-n that had the largest hailstone sequence. You should look at the hailLen() problem before working on this. You should use your solution from the hailLen() problem. ( http://codingbat.com/author/p264289 ) since hailLen(3) is larger than the hailLen of 4 or 5, maxHail of 3,4,5 all return 3. Since 6 has a longer sequence, maxHail(6) gives us 6. remember: Use the hailLen function you already wrote!
Here's my code and the output:
However, I'm not sure where this goes wrong - I checked line-by-line and couldn't see anything wrong. Could anyone help me fix this? Thank you!
...ANSWER
Answered 2022-Mar-22 at 23:12I see what is wrong - hailLen returns lenght of sequence and the question is about index for which the sequence is the longest. Just store it in variable
QUESTION
I am currently doing a problem on coding bat called string_bits and have been debugging it using Thonny and putting the code into coding bat to see if it is correct. Right now I am getting an error with my code in codingbat that says string index out of range. The weird thing is when I run it in Thonny I don't get the error. What is happening here?
...ANSWER
Answered 2022-Jan-15 at 21:39Maybe the test trying some different kinds of input, and not only the obvious. for example if your input is an empty string: it will cause such an "out of range" error. Try to add input check before any operation on the string (which is actually an array)
like so:
QUESTION
I am currently doing an exercise on CodingBat that goes as follows:
Given 2 int values, return True if one is negative and one is positive. Except if the parameter "negative" is True, then return True only if both are negative.
This is my initial attempt:
...ANSWER
Answered 2022-Jan-15 at 01:53if a and b < 0
is functionally equivalent to writing
QUESTION
i wanted to ask what helped you grasp the concept of backtracking better.
I think i understand the idea behind it and recursion well enough, yet, i struggle to make sense of why backtracking leads to the wanted outcome. I tried to "dry run" the code on paper, and understand the program flow better, but to almost no avail.
So, naturally, i have a extremely hard time to come up with my own backtracking solutions.
I think i understand why the base case makes sense, why the if-calls are necessary, and see that every option is being checked (by using a debugger), yet i do not see why java computes the code that way internally.
For example here: https://codingbat.com/prob/p145416:
...ANSWER
Answered 2021-Dec-17 at 20:07i wanted to ask what helped you grasp the concept of backtracking better.
For grasping the general idea, it helped me to watch visualisations of solutions using backtracking (videos or pages with visualisations). For grasping how the calls and recursive calls work, I just did a lot of step through debugging and watched some visualisations as well.
From the comments that you added to your code, I can see, that you already grasped the general idea of backtracking and why those conditions and recursive calls are there.
So how do computers (this is not specific to Java) perform calls and recursive calls and more importantly, how do they keep track of where to return after a call finishes executing?
They use a call stack. From wikipedia
A call stack is used for several related purposes, but the main reason for having one is to keep track of the point to which each active subroutine should return control when it finishes executing. An active subroutine is one that has been called, but is yet to complete execution, after which control should be handed back to the point of call. Such activations of subroutines may be nested to any level (recursive as a special case), hence the stack structure.
A call stack keeps track of calls that are still in progress by pushing (adding) stack frames on the stack every time a call happens and popping (removing) them when an active call returns.
The stack frames contain information about (simplified):
- the values of the parameters of the call
- the return address = the position in code to return to after the call finishes executing
- the local variables of the called method, the context/scope
The stack and the stack frames make recursion calls possible.
I know that you already used a debugger to step through the code while it was executing, but let's do it again here on "paper".
I will use line numbers in your code (I also removed the comments) to make it easier to reference the lines. The method that will be called recursively has 5 lines of code.
QUESTION
Codingbat has a practice question Under Logic-1, Python. It's called near_10.
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
The solution on one user's GitHub is given as
...ANSWER
Answered 2021-Oct-12 at 18:43The solution you provided is indeed false and doesn't work in the slightest. Take a look at the first term ((num/10)*10)
: this evaluates to just num
. This means for all integers greater than 9 your result is num % num
which will always be 0. As well, because this code uses normal division it will convert all terms to floats.
The second solution you provided is correct since all you need is the first no-decimal digit.
QUESTION
I'm practicing code on codingbat and came across this logic question:
Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode is True, in which case return True if the number is less or equal to 1, or greater or equal to 10.
The solution is:
...ANSWER
Answered 2021-Oct-11 at 20:36^
is the bitwise exclusive or (or XOR)
QUESTION
The challenge is from codingbat - Sum the elements of the array except for all values between 6 and 7, not counting the 6 or the 7 either.
My strategy was create a new array "valid" and append valid elements of the array nums to the "valid" array, and then sum the valid array.
Because I need the index to check for a 7 once a 6 is detected, my plan was to enumerate the array, and when the value 6 is found, to loop through the remainder of the array until a 7 is found, not appending the values 6-7 to the array "valid" in the process.
I ran my code through the MIT Python tutor to see where it was going wrong, and when I increment the index after i+1 != 7 (with the intention of checking to see if the next value in the array is a 7) it increments the index (it finishes working with that index of the array and moves on to the next one, rather than sticking in the code for the 6 and continuing to loop through and check for a 7).
Appreciate any help with this, especially a solution that sticks to the original strategy but implements it correctly in the code.
...ANSWER
Answered 2021-Sep-07 at 16:06You can achieve this simply by flagging whether the list is currently between 6 and 7 or not:
QUESTION
The problem statement is as follows:
Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.
sum67([1, 2, 2]) → 5
sum67([1, 2, 2, 6, 99, 99, 7]) → 5
sum67([1, 1, 6, 7, 2]) → 4
I'm aware of the same problem being posted here on stack overflow but I don't want to request a new solution, rather I want to know what might be the problems with my own recursive solution to the problem.
My attempt:
...ANSWER
Answered 2021-Jul-02 at 13:09The recursion limit is 1000 by default. You could use a while true
instead, like:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install codingbat
You can use codingbat like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the codingbat component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page