Store numbers in a list that has a range - for-loop

Write a program that will store the numbers from a list called random_number which are less than or equal to 300 into a new list. Print the results.
random_numbers = [100, 34, 10, 17, 111, 304, 99, 87, 55, 0, 5, 303, 399, 354, 121, 208, 267, 406, 13]
My attempt
rand_count=0
for rand_value in random_numbers:
if rand_value <=300:
print (rand_value) rand_count+=1
else:
random_numbers.append(rand_value)

Related

Efficient calculations of paths with shared subpaths in directed graph

I need a recommendation for an efficient datastructure that suits my problem of calculating the path costs inside a directed graph under the contraint, that many paths sometimes share a same subpath for which I do not want to make the calculations twice.
Each number in these lists are refering to a node inside a directed graph. Each line describes one path:
[121, 85, 135, 99, 141, 134, 4, 33, 65, 131, 18, 127],
[121, 85, 135, 99, 141, 134, 65, 33, 4, 127],
[121, 85, 135, 99, 141, 134, 65, 33, 4, 131, 18, 127],
[121, 85, 135, 99, 141, 134, 65, 33, 4, 107, 127],
[121, 85, 135, 99, 141, 134, 65, 23, 18, 127],
[121, 85, 135, 99, 141, 134, 65, 23, 18, 131, 4, 127],
[121, 85, 135, 99, 141, 134, 65, 23, 18, 131, 4, 107, 127],
[121, 85, 135, 99, 141, 134, 65, 107, 4, 127],
[121, 85, 135, 99, 141, 134, 65, 107, 4, 131, 18, 127],
[121, 85, 135, 99, 141, 134, 65, 107, 127],
[121, 85, 135, 99, 141, 134, 65, 131, 18, 127],
[121, 85, 135, 99, 141, 134, 65, 131, 4, 127],
[121, 85, 135, 99, 141, 134, 65, 131, 4, 107, 127],
[121, 85, 135, 99, 141, 4, 127],
...
As one can see in this example many paths share the same subset of nodes along the way. (And many other paths [not shown here] do not share a subpath.)
I want to compute the 'optimality' of each path, i.e. the sum OR products of the weights along the way. These weights can be seen as constant for my graph while traversing each path.
To be clear: I want to avoid calculating the weight of the path from node 121 to node 141 for all shown paths in this example, as these multiplications / additions of the weights between two nodes is the same (up to node 141) in each of the shown paths...
I am satisfied with a datastructure recommendation and explanation why the mentioned datastructure is best suited for my needs.
If you also have a library recommendation I would prefer one in C/C++ or Python.

Ruby: using `.each` or `.step`, step forward a random amount for each iteration

(Also open to other similar non-Rails methods)
Given (0..99), return entries that are randomly picked in-order.
Example results:
0, 5, 11, 13, 34..
3, 12, 45, 67, 87
0, 1, 2, 3, 4, 5.. (very unlikely, of course)
Current thought:
(0..99).step(rand(0..99)).each do |subindex|
array.push(subindex)
end
However, this sets a single random value for all the steps whereas I'm looking for each step to be random.
Get a random value for the number of elements to pick, randomly get this number of elements, sort.
(0..99).to_a.sample((0..99).to_a.sample).sort
#⇒ [7, 20, 22, 29, 45, 48, 57, 61, 62, 76, 80, 82]
Or, shorter (credits to #Stefan):
(0..99).to_a.sample(rand(0..99)).sort
#⇒ [7, 20, 22, 29, 45, 48, 57, 61, 62, 76, 80, 82]
Or, in more functional manner:
λ = (0..99).to_a.method(:sample)
λ.(λ.()).sort
To feed exactly N numbers:
N = 10
(0..99).to_a.sample(N).sort
#⇒ [1, 5, 8, 12, 45, 54, 60, 65, 71, 91]
There're many ways to achieve it.
For example here's slow yet simple one:
# given `array`
random_indexes = (0...array.size).to_a.sample(rand(array.size))
random_indexes.sort.each { |i| puts array[i] }
Or why don't you just:
array.each do |value|
next if rand(2).zero?
puts value
end
Or you could use Enumerator#next random number of times.
Below example returns a sorted array with random entries from given range based on randomly picked true or false from array [true, false]:
(0..99).select { [true, false].sample }
=> [0, 3, 12, 13, 14, 17, 20, 24, 26, 28, 30, 32, 34, 35, 36, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 58, 59, 60, 61, 62, 65, 67, 69, 70, 71, 79, 81, 84, 86, 91, 93, 94, 95, 98, 99]
To reduce the chances of a bigger array being returned, you can modify your true/false array to include more falsey values:
(0..99).select { ([true] + [false] * 9).sample }
=> [21, 22, 28, 33, 37, 58, 59, 63, 77, 85, 86]

Sort ActiveRecord query response by Globalize3 translation on an association using locale

I have two models and three tables total which are involved. Unfortunately, some renaming of the models from the original table names has occurred, so pardon the confusion.:
Symptom (chief_complaints)
SymptomName (chief_complaint_names)
(chief_complaint_name_translations)
The latter, of course, is used by Globalize3 to translate the :name attribute of the SymptomName model. Symptom has_many :symptom_names.
Consider the following:
Symptom.includes(names: :translations).order("chief_complaint_name_translations.name ASC")
This returns a mostly correct list, but it's sorting the list of Symptoms by the first translation name it encounters (despite my locale), and listing them by the correct locale.
# .to_sql output
"SELECT `chief_complaints`.* FROM `chief_complaints` INNER JOIN `chief_complaint_names` ON `chief_complaint_names`.`chief_complaint_id` = `chief_complaints`.`id` INNER JOIN `chief_complaint_name_translations` ON `chief_complaint_names`.`id` = `chief_complaint_name_translations`.`chief_complaint_name_id` ORDER BY chief_complaint_name_translations.name, chief_complaint_name_translations.name ASC"
# Actual SQL generated in the console
SELECT `chief_complaints`.* FROM `chief_complaints` INNER JOIN `chief_complaint_names` ON `chief_complaint_names`.`chief_complaint_id` = `chief_complaints`.`id` INNER JOIN `chief_complaint_name_translations` ON `chief_complaint_names`.`id` = `chief_complaint_name_translations`.`chief_complaint_name_id` ORDER BY chief_complaint_name_translations.name, chief_complaint_name_translations.name ASC
SELECT `chief_complaint_names`.`id` AS t0_r0, `chief_complaint_names`.`chief_complaint_id` AS t0_r1, `chief_complaint_names`.`created_at` AS t0_r2, `chief_complaint_names`.`updated_at` AS t0_r3, `chief_complaint_name_translations`.`id` AS t1_r0, `chief_complaint_name_translations`.`chief_complaint_name_id` AS t1_r1, `chief_complaint_name_translations`.`locale` AS t1_r2, `chief_complaint_name_translations`.`name` AS t1_r3, `chief_complaint_name_translations`.`created_at` AS t1_r4, `chief_complaint_name_translations`.`updated_at` AS t1_r5, `chief_complaint_name_translations`.`url` AS t1_r6 FROM `chief_complaint_names` LEFT OUTER JOIN `chief_complaint_name_translations` ON `chief_complaint_name_translations`.`chief_complaint_name_id` = `chief_complaint_names`.`id` WHERE `chief_complaint_name_translations`.`locale` = 'en' AND `chief_complaint_names`.`chief_complaint_id` IN (173, 2, 1, 224, 223, 3, 75, 4, 186, 15, 199, 201, 5, 177, 245, 94, 219, 225, 241, 6, 228, 213, 234, 164, 88, 26, 81, 7, 74, 136, 57, 21, 28, 18, 163, 165, 8, 112, 183, 147, 9, 160, 10, 64, 218, 170, 200, 207, 11, 175, 13, 138, 72, 12, 214, 239, 248, 14, 150, 190, 137, 16, 17, 154, 178, 127, 56, 206, 246, 101, 19, 20, 22, 96, 172, 255, 23, 24, 216, 25, 215, 29, 125, 113, 198, 195, 244, 27, 247, 132, 232, 70, 135, 133, 30, 31, 34, 32, 197, 181, 222, 208, 243, 35, 227, 196, 33, 36, 179, 53, 131, 126, 159, 58, 37, 202, 203, 38, 120, 68, 220, 230, 176, 39, 226, 148, 174, 91, 40, 41, 145, 151, 134, 189, 73, 43, 42, 47, 93, 44, 45, 46, 209, 192, 204, 205, 48, 188, 128, 49, 212, 249, 250, 211, 153, 50, 51, 52, 139, 187, 237, 109, 156, 129, 54, 157, 55, 87, 69, 84, 146, 60, 149, 221, 231, 242, 229, 59, 194, 240, 155, 61, 158, 62, 171, 180, 67, 63, 236, 65, 66, 162, 71, 152, 191, 76, 77, 78, 79, 80, 82, 83, 103, 92, 98, 118, 85, 100, 89, 116, 114, 115, 104, 99, 111, 86, 97, 122, 251, 90, 238, 193, 254, 95, 252, 130, 235, 233, 102, 121, 123, 105, 106, 107, 108, 110, 217) ORDER BY name
You'll notice that there is no mention of 'locale' or 'en' in this query. If I do a query on SymptomName, including the with_translations method, passing the locale into it, it writes the query exactly as I expect it would.
>> SymptomName.with_translations(I18n.locale).to_sql
=> "SELECT `chief_complaint_names`.* FROM `chief_complaint_names` WHERE `chief_complaint_name_translations`.`locale` = 'en' ORDER BY name"
How can I properly inject the locale into the first query and have it sort my list of Symptoms according to the associated SymptomNames. It is worth noting that I have a method on Symptom that returns the first SymptomName it encounters.
I need this to sort the list by names derived from the locale AND to show the proper names. Any thoughts?
Thank you!
Just a small update for anyone else using this gem. I was surprised to discover that the gem actually creates a new model on the fly for each model that has a translation. This new model has the same name as the original, appended with ::Translation. So SymptomName would have a SymptomName::Translation model. This model has the translated attributes accessible, such as SymptomName::Translation.first.name.
Unfortunately, this new model does not seem to have direct access back to it's namesake as one might expect. There is no SymptomName::Translation.first.symptom`.
Reflecting on all associations does reveal a relationship to said model from the translation model, but the association is not available through the normal chain.
We ended up creating our own model to access this table called SymptomNameTranslation, adding the appropriate associations and joins as a default_scope. Seems to be working great, but would be nice to have this in the gem itself. Might be a good opportunity for a patch to said gem.

Mathematical representation of large numbers?

I am attempting to write a function which takes a large number as input (upwards of 800 digits long) and returns a simple formula of no complex math as a string.
By simple math, I mean just numbers with +,-,*,/,^ and () as needed.
'4^25+2^32' = giveMeMath(1125904201809920); // example
Any language would do. I can refactor it, just looking for some help with the logic.
Bonus. The shorter the output the better. Processing time is important. Also, mathematical accuracy is a must.
Update:
to clarify, all input values will be positive integers (no decimals)
I think the entire problem can be recast to a run-length encoding problem on the binary representation of the long integer.
For example, take the following number:
17976931348623159077293051907890247336179769789423065727343008115773
26758055009631327084773224075360211201138798713933576587897688144166
22492847430639474110969959963482268385702277221395399966640087262359
69162804527670696057843280792693630866652907025992282065272811175389
6392184596904358265409895975218053120L
This looks fairly horrendous. In binary, though:
>>> bin(_)
'0b11111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111100000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000
0000000'
Which is about 500 ones, followed by 500 zeroes. This suggests an expression like:
2**1024 - 2**512
Which is how I obtained the large number in the first place.
If there are no significantly long runs in the binary representation of the integer, this won't work well at all. 101010101010101010.... is the worst case.
Here is my attempt in Python:
def give_me_math(n):
if n % 2 == 1:
n = n - 1 # we need to make every odd number even, and add back one later
odd = 1
else:
odd = 0
exps = []
while n > 0:
c = 0
num = 0
while num <= n/2:
c += 1
num = 2**c
exps.append(c)
n = n - num
return (exps, odd)
Results:
>>> give_me_math(100)
([6, 5, 2], 0) #2**6 + 2**5 + 2**2 + 0 = 100
>>> give_me_math(99)
([6, 5, 1], 1) #2**6 + 2**5 + 2**1 + 1 = 99
>>> give_me_math(103)
([6, 5, 2, 1], 1) #2**6 + 2**5 + 2**2 + 2**1 + 1 = 103
I believe the results are accurate, but I am not sure about your other criteria.
Edit:
Result: Calculates in about a second.
>>> give_me_math(10**100 + 3435)
([332, 329, 326, 323, 320, 319, 317, 315, 314, 312, 309, 306, 304, 303, 300, 298, 295, 294, 289, 288, 286, 285, 284, 283, 282, 279, 278, 277, 275, 273, 272, 267, 265, 264, 261, 258, 257, 256, 255, 250, 247, 246, 242, 239, 238, 235, 234, 233, 227, 225, 224, 223, 222, 221, 220, 217, 216, 215, 211, 209, 207, 206, 203, 202, 201, 198, 191, 187, 186, 185, 181, 176, 172, 171, 169, 166, 165, 164, 163, 162, 159, 157, 155, 153, 151, 149, 148, 145, 142, 137, 136, 131, 127, 125, 123, 117, 115, 114, 113, 111, 107, 106, 105, 104, 100, 11, 10, 8, 6, 5, 3, 1], 1)
800 digit works fast too:
>>> give_me_math(10**800 + 3452)
But the output is too long to post here, which is OPs concern of course.
Time complexity here is 0(ln(n)), so it is pretty efficient.
In java, you should take a look at the BigDecimal class in java.math package.
I'd suggest you to have a look at
The GMP library (The GNU Multiple Precision Arithmetic Library) for performing the arithmetics
Take a look at integer factorization. The link redirects to Wikipedia which should give probably a good overview. However to be a bit more scientific:
Integer factorization (PDF) by Daniel Bernstein of the University of Illinois
Integer Factorization Algorithms (PDF) by Connelly Barnes of the Department of Physics, Oregon State University

What are some algorithms for finding a closed form function given an integer sequence?

I'm looking form a programatic way to take an integer sequence and spit out a closed form function. Something like:
Given: 1,3,6,10,15
Return: n(n+1)/2
Samples could be useful; the language is unimportant.
This touches an extremely deep, sophisticated and active area of mathematics. The solution is damn near trivial in some cases (linear recurrences) and damn near impossible in others (think 2, 3, 5, 7, 11, 13, ....) You could start by looking at generating functions for example and looking at Herb Wilf's incredible book (cf. page 1 (2e)) on the subject but that will only get you so far.
But I think your best bet is to give up, query Sloane's comprehensive Encyclopedia of Integer Sequences when you need to know the answer, and instead spend your time reading the opinions of one of the most eccentric personalities in this deep subject.
Anyone who tells you this problem is solvable is selling you snake oil (cf. page 118 of the Wilf book (2e).)
There is no one function in general.
For the sequence you specified, The On-Line Encyclopedia of Integer Sequences finds 133 matches in its database of interesting integer sequences. I've copied the first 5 here.
A000217 Triangular numbers: a(n) = C(n+1,2) = n(n+1)/2 = 0+1+2+...+n.
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, 1326, 1378, 1431
A130484 Sum {0<=k<=n, k mod 6} (Partial sums of A010875).
0, 1, 3, 6, 10, 15, 15, 16, 18, 21, 25, 30, 30, 31, 33, 36, 40, 45, 45, 46, 48, 51, 55, 60, 60, 61, 63, 66, 70, 75, 75, 76, 78, 81, 85, 90, 90, 91, 93, 96, 100, 105, 105, 106, 108, 111, 115, 120, 120, 121, 123, 126, 130, 135, 135, 136, 138, 141, 145, 150, 150, 151, 153
A130485 Sum {0<=k<=n, k mod 7} (Partial sums of A010876).
0, 1, 3, 6, 10, 15, 21, 21, 22, 24, 27, 31, 36, 42, 42, 43, 45, 48, 52, 57, 63, 63, 64, 66, 69, 73, 78, 84, 84, 85, 87, 90, 94, 99, 105, 105, 106, 108, 111, 115, 120, 126, 126, 127, 129, 132, 136, 141, 147, 147, 148, 150, 153, 157, 162, 168, 168, 169, 171, 174, 178, 183
A104619 Write the natural numbers in base 16 in a triangle with k digits in the k-th row, as shown below. Sequence gives the leading diagonal.
1, 3, 6, 10, 15, 2, 1, 1, 14, 3, 2, 2, 5, 12, 4, 4, 4, 13, 6, 7, 11, 6, 9, 9, 10, 7, 12, 13, 1, 0, 1, 10, 5, 1, 12, 8, 1, 1, 14, 1, 9, 7, 1, 4, 3, 1, 2, 2, 1, 3, 4, 2, 7, 9, 2, 14, 1, 2, 8, 12, 2, 5, 10, 3, 5, 11, 3, 8, 15, 3, 14, 6, 3, 7, 0, 4, 3, 13, 4, 2, 13, 4, 4, 0, 5, 9, 6, 5, 1, 15, 5, 12, 11, 6
A037123 a(n) = a(n-1) + Sum of digits of n.
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 46, 48, 51, 55, 60, 66, 73, 81, 90, 100, 102, 105, 109, 114, 120, 127, 135, 144, 154, 165, 168, 172, 177, 183, 190, 198, 207, 217, 228, 240, 244, 249, 255, 262, 270, 279, 289, 300, 312, 325, 330, 336, 343, 351, 360, 370, 381
If you restrict yourself to polynomial functions, this is easy to code up, and only mildly tedious to solve by hand.
Let , for some unknown
Now solve the equations
…
which simply a system of linear equations.
If your data is guaranteed to be expressible as a polynomial, I think you would be able to use R (or any suite that offers regression fitting of data). If your correlation is exactly 1, then the line is a perfect fit to describe the series.
There's a lot of statistics that goes into regression analysis, and I am not familiar enough with even the basics of calculation to give you much detail.
But, this link to regression analysis in R might be of assistance
The Axiom computer algebra system includes a package for this purpose. You can read its documentation here.
Here's the output for your example sequence in FriCAS (a fork of Axiom):
(3) -> guess([1, 3, 6, 10, 15])
2
n + 3n + 2
(3) [[function= -----------,order= 0]]
2
Type: List(Record(function: Expression(Integer),order: NonNegativeInteger))
I think your problem is ill-posed. Given any finite number of integers in a sequence with
no generating function, the next element can be anything.
You need to assume something about the sequence. Is it geometric? Arithmetic?
If your sequence comes from a polynomial then divided differences will find that polynomial expressed in terms of the Newton basis or binomial basis. See this.
There is no general answers; a simple method can be implemented bu using Pade approximants; in two words, assume your sequence is a sequence of coefficients of the Taylor expansion of an unknown function, then apply an algorithm (similar to the continued-fraction algorithm) in order to "simplify" this Taylor-expansion (more precisely: find a rational function very close to the initial (and truncated) function. The Maxima program can do it: look at "pade" on the page: http://maxima.sourceforge.net/docs/manual/maxima_28.html
Another answer tells about the "guess" package in the FriCAS fork of Axiom (see previous answer by jmbr). If I am not wrong; this package is itself inspired from the Rate program by Christian Krattenthaler; you can find it here: http://www.mat.univie.ac.at/~kratt/rate/rate.html Maybe looking at its source could tell you about other methods.

Resources