I need to write a function to satisfy this input -> output list:
0 -> 0
1 -> 1
3 -> 2
4 -> 3
5 -> 5
7 -> 13
9 -> 34
f(x) = ??
Well, that is incredibly easy... if you aren't concerned about over-fitting, then you can do:
switch(input)
case 0: report 0
case 1: report 1
case 3: report 2
...
default: report whatever
You probably need more constraints on the problem if you want a good solution. You also might consider graphing the function to see if there is any obvious pattern, or maybe show the bits involved. It would also be useful to know if the inputs and outputs are integer-valued or real-valued (is the function supposed to be continuous or discrete?). Without this information, it's a little bit hard to help.
Edit
Showing the missing numbers helps:
0 -> 0
1 -> 1
2 -> 1
3 -> 2
4 -> 3
5 -> 5
6 -> 8
7 -> 13
8 -> 21
9 -> 34
(It's the Fibonnaci numbers: f(x) = f(x-1)+f(x-2), where f(0)=0 and f(1)=1).
PS
This is a function for which dynamic programming or memoization is particularly useful.
Solved by Eureqa
round(exp(0.4807*input - 0.799938))
I don't know if this is homework or not, but this is an extremely well-known sequence. A (rather large) hint is that f(n) is dependent on f(n-1) and f(n-2). If you're not concerned with just being told the answer, click here. Implementing the sequence is pretty trivially done recursively, but edit your post if you're having trouble with it and specify which part you're stuck on
Related
If I am correct, memoization is associated with READ operations. What is the term which refers to the same technique used for WRITE operations?
Example:
Let's say an app receives the following inputs,
0 1 2 2 2 2 2 3 4 3 3 3 3 3 4 4 4 2 1 2 5 5 5 5 3
Instead of saving everything, we tune the app to save only the transitions. (i.e ignore consecutive duplicates)
0 1 2 3 4 3 4 2 1 2 5 3
What is the (standard) term that can be used to describe the above technique?
I feel bad about using the same term since the final outcome is quite different. In READ operations, if memoization is used, the final outcome will remain the same. But in above example for WRITE operations, the final output is different from the original input.
"Deduplication of adjacent/most-recent entries". Your example looks like what the uniq tool does.
If you preserved the count of duplicates, it would be a form of RLE (run-length encoding).
As an aside, I guess you mean memoization as a way to speed up reads, and this as a method to speed up writes, but I wouldn't say that's the opposite of memoization, since it's opposite of the general goal, but not related to the particular method.
As far as I know, there is no applicable terminology for what you are asking.
And it is NOT memoization ... or (to my mind) the reverse of memorization.
(Likewise, there is no word in English for a cat with three legs.)
Suppose system is evolved by extraterrestrial creatures having only 3 figures and they use the figures 0,1,2 with (2>1>0) ,How to represent the binary equivalent of 222 using this?
I calculated it to be 22020 but the book answers it 11010 .how this.Shouldn't i use the same method to binary conversion as from decimal to binary except using '3' here ???
I think you meant base 3 (not binary) equivalent of decimal 222
22020 in base 3 is 222 in decimal.
220202(your answer) in base 3 is 668 in decimal.
11010 (according to book) in base 3 is 111 in decimal.
222 in binary is 11011110
May be i will be able to tell where you went wrong if you tell the method you used to calculate base 3 equivalent of 222
Edit:
Sorry I could not understand the problem until you provide the link. It says what is binary equivalent of 222 (remember 222 is in base 3)
222 in base 3 = 26 in decimal (base 10)
26 in decimal = 11010 in binary
Mark it as accepted if it solved your problem.
Assuming the start is decimal 222.
Well, without knowing the system used in the book I would decompose it by hand in the following way:
3^4 = 81,
3^3 = 27,
3^2 = 9,
3^1 = 3,
So 81 fits twize into 222 , so the 4th "bit" has the value 2.
Remaining are 60. 27 fits twice into 60 so the next bit is 2 again.
Remaining are 6. 9 fits not into 6, so the next bit is 0.
Remaining are 6. 3 fits twice into 6, so the next bit is 2.
remaining are 0. so the last bit 0
This gives as result 22020.
One quick sanity check on how many "bits" are needed for representation of decimal 222 in a number system with 3 Numbers: 1+log(222)/log(3)=5,9 => nearly 6 "bits" are needed, which goes well with the result 22020.
First see how many figures you have, here we have 3 so
we have to convert 222 to binary when we have only 3 figures so
2×3^2+2×3^1+2×3^0 (if the number were being 121 then →
1×3^2+2×3^1+1×3^0)
which gives 26 then divide this with 2 until we don't get 1/2
when reminder is 1 then write 1 if 0 then 0 you will get
so we get 01011 just reverse it we have the answer
11010
enter image description here
Lately, I have been studying 6502 microprocessor and came across the fact that binary and voltage relate. 0 for 0volts and 1 for 5 volts.
Now I just recently learned about endian-ness as well. So trying to learn more about both of these topics I was wondering if someone could explain the relation of binary/voltage and the little or big endian.
If there really isn't a difference because 00000001 would only use 5 volts and 10000000 would only used 5 volts as well. Then I am sorry for asking a useless topic. Now if that is the case, please share some more interesting knowledge about endian-ness, binary
and/or Voltage.
Unfortunately I don't have university experience so i am unsure if this is common knowledge, but thanks for any information that you provide.
They're not very related.
When you have a voltmeter and you read a single bit, a 0-volt would correspond to a 0, and a 5-volt would correspond to a 1. Or you could say "high voltage is 1, and low voltage is 0".
Now, to represent a number, let's simply say that we use powers of 2:
1 = 001
2 = 010
3 = 011
4 = 100
5 = 101
And so on. However, what I just used is little-endian: the end bit (the one on the right) is small, it represents 1 (if it's 1) or 0 (if it's 0), as opposed to the bit on the left (4 if it's 1, 0 if it's 0). If we flipped the order around, that would be big-endian.
You could think of each bit (each 0 or 1) as a different wire with either 0 or 5 volts on it.
I'm going to make a computer in Minecraft. I understand how to build a computer where it can make binary operations but I want the outputs to be displayed as standard integer numbers. How you "convert" the binaries into standard digits? Is there any chart for that? And the digits will be shown like in old calculators; with 7 lines.
--
| |
--
| |
--
In electronics, what you need is called a "binary to binary coded decimal" converter. "Binary coded decimal" is the set of bits needed to produce a number on a 7 segment display. Here's a PDF describing how one of these chips works. Page 3 of the PDF shows the truth table needed to do the conversion as well as a picture of all of the NAND gates that implement it in hardware. You can use the truth table to build the set of boolean expressions needed in your program.
0 = 0
1 = 1
10 = 2
11 = 3
100 = 4
101 = 5
110 = 6
111 = 7
...
Do you see the pattern? Here's the formula:
number = 2^0 * (rightmost digit)
+ 2^1 * (rightmost-but-1 digit
+ 2^2 * (rightmost-but-2 digit) + ...
Maybe what you are looking for is called BCD or Binary Coded Decimal. There is a chart and a karnaugh map for it that has been used for decades. a quick Google search for it gave me this technical page
http://circuitscan.homestead.com/files/digelec/bcdto7seg.htm
How are you trying to build the computer?
Maybe that key word can at least help you find what you need. :)
Your problem has two parts:
Convert a binary number into digits, that is do a binary to BCD conversion.
Convert a digit into a set of segments to activate.
For the latter you can use a table that assigns the bitmap of active segments to each digit.
I think's that's two different questions.
There isn't a "binary string of 0/1" to integer conversion built in - you would normally just write your own to loop over the string and detect each power of 2.
YOu can also write your own 7segment LED display - it's a little tricky because it's on multiple lines, but would be an interesting excersize.
Alternatively most GUIs have an LCD font,Qt certainly does
I am trying to round up cases when it makes sense to use a map (set of key-value entries). So far I have two categories (see below). Assuming more exist, what are they?
Please limit each answer to one unique category and put up an example.
Property values (like a bean)
age -> 30
sex -> male
loc -> calgary
Presence, with O(1) performance
peter -> 1
john -> 1
paul -> 1
Sparse Data Structures (e.g. a sparse array or a matrix):
0 -> value
1 -> value
100 -> value
105 -> value
Also, I would argue that the "Presence" example you listed is better done with a Set data structure (e.g. HashSet in Java or .NET) since the "mapping" part of the map is really not necessary.
Remembering function results (caching, buffering, memoization)
10 -> 2
20 -> 7
30 -> zeroesIn(factorial(30))
Conversion
peter -> pierre
john -> jean
paul -> paul
If your language allows both associative arrays and pointer to functions/procedures, you can use maps to build something similar to Object Oriented (see Perl for a classical example).
See here for a more detailed explanation.
Passing arbitrary number of optional parameters to a function, in a language which doesn't support them:
cars = findAvailableCars(make -> 'Toyota', model -> 'Prius', color -> 'green')
As Eric Petroelje said, your "presence" example is better suited to a Set than a Map.
However, if you want to keep track of the number of occurrences of things, use a Map. For example, you want to know how many times a given word appears in a document:
pseudocode:
wordMap = map()
for word in document:
if wordMap.containsKey(word):
wordMap[word]++
else:
wordMap[word] = 1
then if I want to know how many times the word 'map' appears in the document, it would just be wordMap["map"]
A Map is one way of representing a graph. The keys are the nodes in the graph, and the value for a particular node N is a list of all the nodes that N connects to.
(Thanks for the retag, MatrixFrog.)
Dictionary (mapping a term to a definition)
"postulate" -> "demand or claim"
"consulate" -> "residence of a foreign official"
Also in this category
EADDRINUSE -> "Address in use."
EADDRNOTAVAIL -> "Address not available."