Here's my problem:
I need to create a program in Scheme to play the "What number am I thinking of" game
The parameters are the lower limit and the upper limit
Every time a guess is taken, it is compared to the answer
if the guess is less, the it should say-go higher
if it's more, it should say -go lower
if it's equal, the print out "Winnner!".
Here is my current code:
(define (game low high)
(let ((guess (average low high)))
(cond ((islow? guess) (game (+ guess 1) high))
((ishigh? guess) (game low (- guess 1)))
(else '(Winner!)))))
How do I implement islow and ishigh?
You can do something like a binary-search.
while (true){
response = guess(number);
if(response == true) return "Found it!";
if(response == 'higher'){
number = number+maximum/2;
}
else if(response == 'lower'){
number = number+minimum/2;
}
}
I might be misinterpreting your assignment – if so, let me know.
Your function doesn't actually know what the correct answer is! This might be why you're confused about islow? and ishigh?...
So I think the first thing your function needs to do is come up with the correct answer, which will be a number between low and high. You can do this with the (random k) function, which picks a random number between 0 and k-1. The first problem is to figure out how to use that to get a number between low and high.
Once you have the answer, then islow? and ishigh? are straightforward, and I don't think you need to make separate functions for them; just use > and <.
You also need to let the user input a guess repeatedly, right? It might be easiest if for now, you just say that you're going to make a function get-guess that'll ask the user for input, and then figure that out later.
Related
I am making a simple game in LEGO EV3 programmer (default software) and I have a code that I don't know how I'd make.
I have 2 variables -
if player is in the right spot,
if bullet is in the right spot.
I want to make it so it is compared, and if the variables both equal, and are in the same position, it makes another variable true.
Therefore...
if ((IF PLAYER & IF OBJECT) = SAME) & ((IF PLAYER = TRUE) & (IF OBJECT = TRUE)) then VARIABLE = TRUE;
Or something like that...
Your code should not have "if if" anywhere, that just looks wrong.
What do you mean by "if player = true"? This doesn't make sense when you read it. A player cannot be true.
The code should look like if player = bullet then shot = true. This assumes that the player and the bullet variables represent the location of these objects. (I know nothing about Mindstorms, by the way.) Maybe you need to replace the first = with ==, depending on how the programming language needs it.
What language that you use?
I am used python to wrote code in my brick before.
So I may code as this:
spotA = player.spot()
spotB = object.spot()
anotherVer = false
if spotA == spotB:
anotherVer = true
And also, I think in all languages, that relational expression is use like
==
!=
||
&&
etc.
Hope this will help you. :)
Before I reload a table I want to know that all of the tasks that were saving in the background are done saving. I have 5 different saveInBackground functions that must complete before I make a new PFQuery. My thought right now is to say:
object.saveInBackgroundWithBlock({ (Success: true, error) -> Void in
count ++
})
Then, once the count gets to a certain number to perform the query. I don't want to use if because if the network is taking too long to process the save request. Would a while statement work? Such as:
while count < 0 {
continue
} else if count == 5 {
self.fetchSecondaryObjects()
}
Ideally I would be using a real-time data engine to power my application but I'm having trouble locating one that I can use as I only write in Swift. If this exists out there and you know of it, I'd love to know about it.
Just do the check each time you increment. It's really very low overhead, only five comparisons.
object.saveInBackgroundWithBlock({ (Success: true, error) -> Void in
count ++;
if count == 5 {
self.fetchSecondaryObjects()
}
})
Unless I'm missing some reason you can't do that, it seems like the simplest solution. Better than running an infinite loop.
Generally I try not to use while in such cases because one can easily get stuck in them if you're somehow of in your logic. Why don't you just use something like that:
if count >= xyz
I'm relatively new to Matlab, and I've been trying to solve my problem for ages but I'm just continuously arriving at a dead end.
I have a code which should, in theory, play 3 sounds in a random order (each order being different for each trial). Upon each sound playing the participant will be asked which sound they heard and then given feedback. I have all the code complete and working up until the random order part. I have created code that on each trial will randomly order 1,2 and 3.
Order = [1, 2, 3];
PhonemeOrder = randperm (numel(Order));
I then have a function which plays the sound/asks the questions etc. within this I have attempted switch cases statements and if else statements depending on the number that PhonemeOrder produces but the order doesnt change even when phoneme order does. I believe my problem is however that PhonemeOrder comes out like [1,2,3] or [3,1,2] which is what i wanted. but Im not sure how to get my sounds to play in the order that it shows because I am using code like...
if/ PhonemeOrder = 1;
then do this...
elseif phonemeorder = 2;
then do this...
else
do this...
Or I've tried code like
switch cases
case 1
do this
case 2
do this
case 3
do this
I'm guessing this is where i am going wrong, but i just dont know how to change it and make it work! I hope this makes sense? I just need it to play in the order that phonemeorder specifies, with the order changing on each trial.
Any help will be greatly appreciated :D
bexG,
I think you are on the right track.
The only thing you need is to use a "for-loop" to go through the array of PhonemeOrder.
for i=1:length(PhonemeOrder)
switch PhonemeOrder(i)
case 1
play the first song
case 2
play the second song
case 3
play the thrid song
end
end
I hope this will help.
Please let me know if you have any further question.
I understand how to define an array range in CoffeeScript
lng[1..10]
However if I have
data = 10
What's the best way to find if 10 is within a range of 1 and 11?
if data is between(1..11)
return true
There is no "between" keyword, but you can utilize a normal array-range:
if data in [1..11]
alert 'yay'
But that's a bit of an overkill, so in simple cases I'd recommend a normal comparison:
if 1 <= data <= 11
alert 'yay'
If you don't mind polluting the native prototypes, you can add a between method to the Number objects:
Number::between = (min, max) ->
min <= this <= max
if 10.between(1, 11)
alert 'yay'
Although i personally wouldn't use it. if 1 <= something <= 11 is more direct and everyone will understand it. The between method, instead, has to be looked up if you want to know what it does (or you'd have to guess), and i think it doesn't add that much.
Am i right in thinking that it is not possible to perform insertion sort on a singly linked list?
My reasoning: assuming that insertion sort by definition means that, as we move to the right in the outer loop, we move to the left in the inner loop and shift values up (to the right) as required and insert our current value when done with the inner loop. As a result an SLL cannot accomodate such an algorithm. Correct?
Well, I'd sound like the Captain Obvious, but the answer mostly depends on whether you're ok with keeping all iterations directed the same way as elements are linked and still implementing the proper sorting algorithm as per your definition. I don't really want to mess around your definition of insertion sorting, so I'm afraid you'd really have to think yourself. At least for a while. It's an homework anyway... ;)
Ok, here's what I got just before closing the page. You may iterate over an SLL in reversed direction, but this would take n*n/2 traversals to visit all the n elements. So you're theoretically okay with any traversal directions for your sorting loops. Guess it pretty much solves your question.
It is doable and is an interesting problem to explore.
The core of insertion sort algorithm is creating a sorted sequence with the first one element and extending it by adding new element and keeping the sequence is still sorted until it contains all the input data.
Singly linked list can not be traversed back, but you can always start from it's head to search the position for the new element.
The tricky part is when inserting node i before node j, you must handle their neighbor relationship well(I mean both node i and j's neighbor needs to be taken care of).
Here is my code. I hope it useful for you.
int insertSort(Node **pHead)
{
Node *current1 = (*pHead)->next;
Node *pre1 =*pHead;
Node *current2= *pHead;
Node *pre2=*pHead;
while(NULL!=current1)
{
pre2=*pHead;
current2=*pHead;
while((current2->data < current1->data))
{
pre2 = current2;
current2 = current2->next;
}
if(current2 != current1)
{
pre1->next=current1->next;
if(current2==*pHead)
{
current1->next=*pHead;
*pHead = current1;
}
else
{
pre2->next = current1;
current1->next = current2;
}
current1 = pre1->next;
}
else
{
pre1 = pre1->next;
current1 = current1->next;
}
}
return 0;
}