CodeKata | Learning Scala | Build Tool library
kandi X-RAY | CodeKata Summary
kandi X-RAY | CodeKata Summary
My code Kata solutions in Java and Scala. Learning Scala in the process.
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 CodeKata
CodeKata Key Features
CodeKata Examples and Code Snippets
Community Discussions
Trending Discussions on CodeKata
QUESTION
So i'm currently working on some pricing logic where the situations are:
- 1 glove costs $2.50, 2 gloves costs $4, therefore applying a 20% discount.
- If person buys 3 gloves then it should be the $4 + original price of $2.50 making total $6.50
- If person buys 4 gloves then it should be $8.
- 1 gum costs $0.65, you can buy 6 for the price of 5, so $3.25 for 6 instead of $3.90
- If person buys 7, then it should be 6 for $3.25 + original price of $0.65 totalling $3.90
- If person buys 8, then it should be 6 for $3.25 + (original price of $0.65 * 2) totalling $4.55
So it's very important at what index/stage that the discount needs to be applied by
I took my logic inspiration from this article: http://codekata.com/kata/kata01-supermarket-pricing/
I took my code inspiration from this article: https://github.com/raddanesh/Kata01
Specifically the VolumePricingStrategy as show in this diagram:
Here is my code attempt given the logic i want to achieve:
...ANSWER
Answered 2020-Nov-12 at 16:05Both situations require pretty much the same logic -- there's an individual price, and a group price, and any item that doesn't fit in a group gets the individual price.
So my approach would be to just use that as the basis for the logic. Write a function that takes as an input the qty, the group-qty, the individual price and the group price, like so:
QUESTION
I have problem with solution in this example Say Me Please Operations(codeKata):
You have a string with N numbers, every 2 numbers after an operation(?) return next number in this string. Write a function who return new string with respective operations :
1)addition,
2)subtraction,
3)multiplication,
4)division.
...ANSWER
Answered 2020-Jan-13 at 00:13Let's say v.length
is 5
.
You want to execute the following loops passes:
- i=0, j=1, k=2
- i=1, j=2, k=3
- i=2, j=3, k=4
But that's not what you're doing. You have j
and k
change semi-independently of i
.
- i=0, j=1, k=2
- i=0, j=1, k=3
- i=0, j=1, k=4
- i=0, j=2, k=3
- i=0, j=2, k=4
- i=0, j=3, k=4
- i=1, j=2, k=3
- i=1, j=2, k=4
- i=1, j=3, k=4
- i=2, j=3, k=4
To get the desired outcome, you should only have one loop. You can derive the other values from the loop variable.
You could derive j
and k
from i
as follows:
QUESTION
I completed my algorithm in python3 to solve a "codekata" on codewars.com.(https://www.codewars.com/kata/odder-than-the-rest-1/train/python) My code solves the initial test cases correct and with sufficient speed. However, when submitting it for the final test, the code gets a timeout for taking too long to solve the given tasks.
Initially I had the content for the for-loop implemented as a separat function called "calculate_oddness_level". I found out this could be a bit slower than just doing the implemention inside the for-loop,so i just did that.
I am aware that performing a while loop inside a for loop is not optimal performance-wise. I thought about how to avoid this several times. However I need do determine the "oddness" level for every odd number in my list. For determining the "level" a loop is needed and seemed faster than a recursion. So its seems there was no other option than to run a while-loop inside the for-loop. I however eliminated all even numbers from running a loop by just assigning them "oddness-level" 0 right away.
Therefore I dont know how I could achieve any significant improvements in performance.
...ANSWER
Answered 2019-Sep-25 at 12:49You don't need a list, you could do it with O(1) additional space.
It is likely that not your implementation is the problem, but -1. Why? It is infinitely odd, because (-1 - 1) / 2 = -1. Interestingly this is the only such number, all other negative numbers have an oddity level of 1.
For positive numbers you are basically counting the number of consecutive bits set to 1 from the right, so you can work with masks to be even faster.
This is how such an implementation could look like, assuming 32-bit integers (sorry Java):
QUESTION
Basically I was doing a code kata on codewars site to kinda of 'warm up' before starting to code, and noticed a problem that I don't know if its because of my code, or just regular thing.
...ANSWER
Answered 2019-Jul-18 at 12:01Part of the reason is that the queue code is way faster than the List
code, because queues are optimised for deletes due to the fact that they are a circular buffer. Lists aren't - the list copies the array contents every time you remove that first element.
Change the input value to 72307000
for example. On my machine, the queue finishes that in less than a second. The list is still chugging away minutes (and at this rate, hours) later. In 4 minutes i
is now at 752408 - it has done almost 1% of the work).
Thus, I am not sure the queue is less memory efficient. It is just so fast that you run into the memory issue sooner. The list almost certainly has the same issue (the way that List
and Queue
do array size doubling is very similar) - it will just likely take days to run into it.
To a certain extent, you could predict this even without running your code. A queue with 7230702951 entries in it (running 64-bit) will take a minimum of 8 bytes per entry. So 57845623608 bytes. Which is larger than 50GB. Clearly your machine is going to struggle to fit that in RAM (plus .NET won't let you have an array that large)...
Additionally, your code has a subtle bug. The loop can't ever end (if n
is greater than int.MaxValue
). Your loop variable is an int
but the parameter is a long
. Your int
will overflow (from int.MaxValue
to int.MinValue
with i++
). So the loop will never exit, for large values of n
(meaning the queue will grow forever). You likely should change the type of i
to long
.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install CodeKata
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