Count Occurence and Compute the Sum of Positive Number Until 0 is Reached in Racket - racket

Create a Racket procedure compute_pos that reads numbers from the keyboard until 0 is read, count how many positive numbers are read, and also compute the sum of only positive numbers, and display the count and the sum of positive numbers in different lines. Note that 0 is not included in the computations.
For example, if user enters:
3
6
-4
12
15
-9
-24
4
-3
-5
1
0
then your procedure should print
positive count: 6
positive sum: 41
You need to utilize “display” procedure to display the count and the sum, and also utilize “read” procedure to keep reading the next number from the keyboard.
I don't even know where to start so thanks for any help.

Related

How many words fit in 4 bits

I have 2 questions that I would like to get helped with.
First question is: If you have 4 bits, how many unique numbers can you write?
Second question is: If you have 4 bits, what is the largest number you can write. Answer with the base 10
Appreciate any help!
Edited - nothing really
Answer 1) With the 4 bits we can write 16 different numbers. As we have 4 different position of bits let's say ABCD where A,B,C,D are representing 1 bit. Each position A,B,C,D has two possible input 0 or 1 so each position is having 2 possible inputs.
So for 4 positions total different outputs = 2*2*2*2 =16 Which also can be understood by permutation which is equal to 2^(no.of bits) .Here 2^(4) which is equal to 16.
Answer 2) Mamimum number having n bits is always all bits as 1s so for n bit number, maximum number which can be formed is all n 1s.
So for 4 bit number, Maximum number which can be formed is 4 1s in binary representation of the number. So here that number is 1111 in binary . In decimal it is 15.
It also can be calculated by 2^(no.of bits) -1. Here number of bits is 4 so maximum number is 2^(4) -1 which is 16-1= 15
Is this your college assignment or sth?
The answer is simply,
if you have n bits, you can have 2^n unique numbers.
The largest number you can write is n 1's.
i.e. For four bits,
Number of unique numbers = 2^4 = 16
The largest number = 4 1's = 1111 = F (hex) = 15 (Base 10)

Brainfuck. How check on palindrome?

The problem is to check if sequence is palindrome using Brainfuck.
input is a sequance of numbers
output 0 if it is not palindorme, else 1.
I have one idea:
Say, we have sequance 1 2 3 2 1. We can memorise first cell from our array in variable(do this using operation '!'),
then change 1 to 0(do this using operation '0'),array will be 0 2 3 2 1,
then we go to the end of array until we meet 0 (do this using
'>[>]'),
then we take number from variable and get sequance 0 2 3 2 1 1.
Next step should be to compare two last numbers, if they are equal continue algo from begining else do something...
I do not know how to implement last step.
please excuse me if I won't write the entire program in brainfuck,
This is the main idea:
Read input (pointer should be at last character afterwards)
memorize character
Set value to 0
Go to first [<]
Compare to memorized character (see Brainfuck compare 2 numbers as greater than or less than)
If not equal, print 1
If the next (>) cell array is 0, print 0
Move to pointer to end [>]
Go back to step 2

Maximum Subarray variation

I have to solve a problem much like the maximum subarray problem. I have to find the largest subarray whose average is bigger than k. I thought the following trick. I can transform my array A[] of size n to a B[] where B[i] = A[i] - k. So now the average must be >0. But average greater than zero doesnt simply mean sum greater than zero? So I can directly apply Kadane's algorithm. Am I right? (always under the constraint that there is 1 positive value)
no, kadane's algorithm will still find you the subarray with the biggest sum...i have to solve the same problem. so far i kave find that if we create the array B as you mentioned above and then make the array C which contains the partial sums of the array B,then the maximum interval (i,j) that we are lookink for has the same number for i and j!!! for example:
array A is: 1 10 -1 -1 4 -1 7 2 8 1 .....and the given k is 5 then
array B is: -4 5 -6 -6 -1 -6 2 -3 3 -4
array C is:-4 1 -5 -11 -12 -18 -16 -19 -16 -20
so the subarray that we are looking for is [7,2,8], has length 3, and has the same first and last element which is -16!!!!
edit: i forgot to tell that we are searching for a O(n) or an O(n*logn) algorithm.... #lets_solve_it you are right but your algorithm is O(n^2) whitch is way to big for the data we want to handle. i 'm close to solve it with the function map in c++,whitch is something like a hash table. i thing this is the right diredtion because here the elements of the array C have direct relation with their indexes! Also our professor told us that another possible solution ,is to make again the array C and then take a (special?) pivot to do quicksort....but i don't totally understand what we expect from quicksort to do.
#panos7:
after you have created array C (partial sums array), you seek two values of C, Ci and Cj,
such that, Cj>=Ci, and, (j-i) is as "big" as possible. (j-i) --> MAX.
then return j-i.
in your example, -16>=-18 so you returned j-i=9-6=3
which is the correct answer!

how to make one layer perceptrons learn to show seven segments?

I was asked to write an algorithm to make seven one layer perceptrons learn to show seven segments number according to 4 0-1 inputs, for example
-1 -1 -1 -1 ==> 1 1 1 1 1 1 -1 % 0
-1 -1 -1 1 ==> -1 -1 -1 -1 1 1 -1 % 1
...
can any one help me, please
So, if I'm interpreting this correctly, you give your net a binary representation of a digit and you want it to tell you what line segments are needed to display that digit seven-segments style.
Luckily, since there are only 10 digits, you can just hand write a training set where each digit is correctly matched to the segments needed, and then use the standard perceptron training algorithm: the delta rule.
This algorithm will change the weights of the network until every input pattern is associated with the correct output pattern.
Implementation note: make sure all 4 input units are connected to all 7 output units, and that all of the connection weights start out at some small random value.

Linear Probing in Open Addressing

I have an array with size m = 11 and my hash function is Division method : h(k) = k mod m
I have an integer k = 10 and 10 mod 11 is -1 so where should I put this key in the array? I should put this key in the slot which its index is 10?
please help me thanks
EDITED : for getting my answer well for example I have integers like k = 10,22,31,4,15,28,17,88,59
the array would be like this?thanks
10 9 8 7 6 5 4 3 2 1 0 index
10 31 59 17 28 4 15 88 22 keys
As it's usually done, 10 mod 11 is 10, so yes, you'd normally use index 10.
Edit: To generalize: at least as it's normally defined, given two positive inputs, a modulo will always produce a positive result. As such, your questions about what to do with negative results don't really make sense with respect to the normal definition.
If you really do have the possibility of getting a negative result, my immediate reaction would be to switch to some language that will produce a reasonable result. If you can't do that, then you'd probably want to move the value into the correct range by adding m to the negative number until you get a number in the range [0..m) so it fits the normal definition of mod, then use that as your index.