exercises | simple programs in C , Python , and Haskell
kandi X-RAY | exercises Summary
kandi X-RAY | exercises Summary
A collection of simple programs written to learn various languages and their strengths and weaknesses. Each program is implemented in several languages against a well defined specification. A test harness is used against each implementation to ensure it complies with the spec. Each exercise is in a separate directory with its own README.
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 exercises
exercises Key Features
exercises Examples and Code Snippets
Community Discussions
Trending Discussions on exercises
QUESTION
I am trying to learn programming and in our school we have exercises which are automatically checked by a bot. The time limit is 1 second and the memory limit is 1024 mb. I've tried sorting the array in an ascending order and then multiplicating the 2 highest numbers but that was too slow(my sorting algorithm could be slow so if possible suggest a sorting algorithm.) This is the fastest way that I've been able to do:
...ANSWER
Answered 2022-Apr-09 at 15:29You don't need to sort the entire array - you just need the two largest positive numbers and the two smallest negative numbers. Everything in between is inconsequential.
Instead, you can go over all the input and keep track of the two largest positive numbers and two smallest negative numbers.; At the end of the iteration, multiply each pair (if found), and compare the results.
QUESTION
I have three collections:
Collection trainingPlan:
...ANSWER
Answered 2022-Mar-13 at 23:40I think this does what you want, and hopefully there's an easier way. I think a puzzle piece you hadn't considered yet was "$mergeObjects"
.
QUESTION
I'm a c++ beginner and now reading the C++ Primer. I have some problem about the destrucor:
in chapter 13.1.3: "In a destructor, there is nothing akin to the constructor initializer list to control how members are destroyed; the destruction part is implicit. What happens when a member is destroyed depends on the type of the member. Members of class type are destroyed by running the member’s own destructor. The built-in types do not have destructors, so nothing is done to destroy members of built-in type."
So if I have such a class:
ANSWER
Answered 2022-Feb-18 at 14:11What the spec means is that no code is run to clean up int i
. It simply ceases to exist. Its memory is part of Foo and whenever a Foo instance is released, then i
goes with it.
For pointers the same is true, the pointer itself will simply disappear (it's really just another number), but the pointer might point at something that needs to also be released. The compiler doesn't know if that is the case, so you have to write a destructor.
This is why things like std::shared_ptr
exist; they are clever pointers (aka 'smart') and the compiler does know if it points at something that needs to be released and will generate the correct code to do so. This is why you should always use smart pointers instead of 'naked' ones (like int *p
).
QUESTION
Chapter 7 in FP in Scala deals with creating a purely functional library for handling concurrency. To that end, it defines a type
...ANSWER
Answered 2022-Jan-18 at 14:14Let's take some List(a, b, c)
. After passing it into sequenceRight
it will turn into:
QUESTION
I've coded the "classical" bank account kata with F# MailboxProcessor
to be thread safe. But when I try to parallelize adding a transaction to an account, it's very slow very quick: 10 parallel calls are responsive (2ms), 20 not (9 seconds)! (See last test Account can be updated from multiple threads
beneath)
Since MailboxProcessor
supports 30 million messages per second (see theburningmonk's article), where the problem comes from?
ANSWER
Answered 2022-Jan-15 at 13:55Your problem is that your code don't uses Async all the way up.
Your Account class has the method Open
, Close
, Balance
and Transaction
and you use a AsyncReplyChannel
but
you use PostAndReply
to send the message. This means: You send a message to the MailboxProcessor with a channel to reply. But, at this point, the method waits Synchronously to finish.
Even with Async.Parallel
and multiple threads it can mean a lot of threads lock themsels. If you change
all your Methods to use PostAndAsyncReply
then your problem goes away.
There are two other performance optimization that can speed up performance, but are not critical in your example.
Calling the Length of a list is bad. To calculate the length of a list, you must go through the whole list. You only use this in Transaction to print the length, but consider if the transaction list becomes longer. You alway must go through the whole list, whenever you add a transaction. This will be O(N) of your transaction list.
The same goes for calling (List.sum). You have to calculate the current Balance whenever you call Balance. Also O(N).
As you have a MailboxProcessor, you also could calculate those two values instead of completly recalculating those values again and again.Thus, they become O(1) operations.
On top, i would change the Open
, Close
and Transaction
messages to return nothing, as in my Opinion, it doesn't make sense that they return anything. Your examples even makes me confused of what the bool
return
values even mean.
In the Close
message you return state.Opened
before you set it to false. Why?
In the Open
message you return the negated state.Opened
. How you use it later it just looks wrong.
If there is more meaning behind the bool
please make a distinct Discriminated Union out of it, that describes the purpose of what it returns.
You used an option
throughout your code, i removed it, as i don't see any purpose of it.
Anyway, here is a full example, of how i would write your code that don't have the speed problems.
QUESTION
I have a state with the following structure. It contains a list of Workouts and each workout has a list of exercises related to this workout. I want to be able to do 2 things:
- add new exercises to the specific workout from the list of workouts
- delete a specific exercise from the specific workout
E.g. In my UI I can add new exercises to Workout with the name Day 2
.
So my action payload gets 2 params: workout index (so I can find it later in the state) and exercise that should be added to/deleted from the list of exercises of the specific workout.
State
...ANSWER
Answered 2021-Dec-24 at 23:32Please, try something like the following (I included comments within the code, I hope it makes it clear):
QUESTION
As a Rust beginner working on one of the first problems on Exercism/Rust (https://exercism.org/tracks/rust/exercises/assembly-line)
I would like to know if it is possible to constrain integer input to a range at compile-time
to be able to have a clean set of match expression
cases.
Below is my current implementation of production_rate_per_hour
:
ANSWER
Answered 2021-Dec-13 at 19:15There is currently no way to express this in the type system.
I assume you mean min instead of max. The typical approach would be:
QUESTION
I am currently trying to write an integration test for my repository layer that tests if I call a method, getExercises()
, then it returns List
, provided that the data is loaded into the local Firestore emulator ahead of time.
So far I got the local Firestore emulator to switch on and off at the beginning/end of a test run, respectively. I am able to populate my data into Firestore, and see the data in the local Firestore emulator via the web UI.
My problem is that my test assertion times out because the Task
(an asynchronous construct the Firestore library uses), blocks the thread at the await()
part in the repository method.
ANSWER
Answered 2021-Nov-25 at 17:56This problem has been resolved in a new version of the kotlinx-coroutines
package (1.6.0-RC). See my github compare across branches. Tests now pass as expected with this version.
QUESTION
I'm am working my way through the exercises of the first chapter of The C Programming Language and while I understand most of what is said and shown, there is one example that I don't understand.
In 1.9, there is a function shown to return the length of a line while setting a char array, passed as an argument, to the contents.
...ANSWER
Answered 2021-Oct-30 at 10:28The for
loop is exited if either c
equals EOF
or c
equals '\n'
. Therefore, immediately after the for
loop, if you want to know which value c
has, you must test.
QUESTION
I'm trying to do the rustlings course and I don't understand the error I'm getting for the following code:
...ANSWER
Answered 2021-Oct-20 at 09:22In rust only the last expression is taken as return value.
In your case:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install exercises
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