I've an variable as page number (page) whose values increment by one each time. [Page numbering] But, now I need to customize this numbering to 1,1,2,2,3,3..
Can you suggest any formula for generate this kind of series?
EDIT: (Answer)
After playing with macros and VBA for some time I've figured out a way to generate this type of series for MS word page numbers. This can be easily done with formulas and {Page} variable in word with formula-
{=(({PAGE} + MOD({PAGE},2))/2)}
The answer is simple: (n + 1) / 2
javascript, adapt to suite:
for(i=0; i>yourMaximum; i++){
WriteSomewhere(i + "," + i);
if(i != i - yourMaximum) WriteSomewhere(",");
}
You can do this kind of thing:
for (int i = 0; i < (pages * 2); i++) {
System.out.println((i / 2) + 1);
}
It is late, but it might help someone.
A mathematical answer to the problem:
You do not need to search through all n numbers in order to have a specific result
1 2 3 4 5 6 7 8 9 . . . . . . . n
1 1 2 2 3 3 4 4 5 . . . . . . . f(n)
General formula:
f(n) = ( n - ( (-1) + (-1)^n )/2 )/2
Playing with the first (-1) you can shift the results like this:
f(n) = ( n - ( (3) + (-1)^n )/2 )/2
1 2 3 4 5 6 7 8 9 . . . . . . . n
0 0 1 1 2 2 3 3 4 . . . . . . . f(n)
Python:
(int(x/2+1) for x in itertools.count())
Ruby
(1..10).map {|n| [n,n]}.flatten
=> [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
or
(1..10).inject([]) {|m,n| m<<n<<n}
=> [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
or
(1..10*2).map {|n| (1+n)/2}
=> [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
C#, not a formula but a simplistic algorithm.
int[] pages = new int[2*N];
for(i=0; i<N; i++)
{
page[2*i] = i+1;
page[2*i+1] = i+2;
}
After playing with macros and VBA for some time I've figured out a way to generate this type of series for MS word page numbers. This can be easily done with formulas and {Page} variable in word with formula-
{=(({PAGE} + MOD({PAGE},2))/2)}
Related
I try to solve this problem:
initial list = [0, 1, 2, 2]
You get this sequence of numbers [0, 1, 2, 2] and you need to add every time the next natural number (so 3, 4, 5, etc.) n times, where n is the element of its index. For example, the next number to add is 3, and list[3] is 2, so you append [3] 2 times. New list will be: [0, 1, 2, 2, 3, 3]. Then the index of 4 is 3, so you have to append 4 three times. The list will be [0, 1, 2, 2, 3, 3, 4, 4, 4] and so on. ([0, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10])
In order to solve this, I tried various approaches. I used recursion, but a recursive approach is very slow in this case. I tried as well the mathematical formula from OEIS (A055086) => a(n) = ceiling(2*sqrt(n+1)) - 2. The problem with the formula is that after 2 ** 20 it is too imprecise.
So, my next idea was to use memoization:
lst = [0, 1, 2, 2]
from itertools import repeat
def find(n):
global lst
print(lst[-1], n, flush = True)
if len(lst) > n:
return lst[n]
for number in range(lst[-1]+1, n+1):
lst += list(repeat(number, lst[number]))
if len(lst) > n:
return lst[n]
Now, this approach works until 2 ** 37, but after this is just timing out. The site where I try to implement my algorithm is (https://www.codewars.com/kata/5f134651bc9687000f8022c4/train/python). I don't ask for a solution, but for any hint on how to optimize my code.
I googled some similar problems and I found that in this case, I could use the total sum of the list, but is not very clear to me yet how could this help me.
Any help is welcomed!
You can answer it iteratively like so:
def find(n):
lst = [0,1,2,2]
if n < 4:
return lst[n]
to_add = 3
while n >= len(lst):
for i in range(lst[to_add]):
lst.append(to_add)
to_add += 1
return lst[n]
You could optimise for large n by breaking early in the for loop, and by keeping track of the list length separately, rather than calls to len
using Python 3.8
I have a list and I'm trying to ask for 3 inputs from the user e.g
list = [7, 5, 3, 2, 2, 1]
get 3 inputs e.g 8, 7, 3
Then place them in order e.g [8, 7, 5, 3, 3, 2, 2, 1]
So far I attempted like below (just a test), but it adds 7 to the end instead of next to the other 7. I'm just very confused. I know I can use sort, but that's too easy.
natural = [7, 5, 3, 3, 2]
entry = 7
for item in natural:
if entry >= item:
natural.insert(item, entry)
break
print(natural)
Thanks for any help!
you probably need to define your own reverse_insert function like this:
(to simulate bisect module)
def reverse_insort(a, x, lo=0, hi=None):
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if x > a[mid]: hi = mid
else: lo = mid+1
a.insert(lo, x)
lst = [7, 5, 3, 3, 2]
x = 7
reverse_insort(lst, x)
print(lst)
Output:
[7, 7, 5, 3, 3, 2]
I was hoping to take the average of a list using another list with the start and stop indices.
for example:
a = [3, 9]
b = [0, 1, 12, 9, 0, 8, 9, 3, 3, 5, 7, 1, 4, 6, 6]
I want to take the average of the numbers from b[3] to b[9] and this is what I have so far
counter = a[0]
sum = b[counter]
while counter < a[1] + 1:
counter += 1
sum = sum + b[counter]
denominator = a[1] - a[0] + 1
avg = sum/denominator
But after checking, it seems to be giving the wrong thing
you could use statistics.mean
from statistics import mean
a = [3, 9]
b = [0, 1, 12, 9, 0, 8, 9, 3, 3, 5, 7, 1, 4, 6, 6]
mean(b[a[0]: a[1] + 1])
or you could use:
sum(b[a[0]: a[1] + 1]) / len(b[a[0]: a[1] + 1])
I would suggest the following:
from statistics import mean
a = [3, 9]
b = [0, 1, 12, 9, 0, 8, 9, 3, 3, 5, 7, 1, 4, 6, 6]
avg = mean(b[a[0]:a[1]+1])
print (avg)
When using a list, I saw that I cannot add or subtract the sample I took from the list. For example:
import random
x = random.sample ((1 ,2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), k=1 )
print(x + 1)
Why I can’t add into the list I created and how can I get around that issue?
If you want to increase the value of every item in a list, you can do like:
import random
x = random.sample ((1 ,2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), k=3 )
print(x)
for index in range(len(x)):
x[index] = x[index] +1
print(x)
In your case, if k is always 1, you can simply like:
import random
x = random.sample ((1 ,2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), k=1 )
print(x)
x[0] = x[0] + 1
print(x)
The reason you can't concatenate is because the type random.sample is returning is a list of size k=1. If you want to be returning an element of your sequence and add to it, you should be using random.choice. It should read something along the lines of:
import random
x = random.choice((1,2,3,4,5,6,7,8,9,10,11,12,13))
print(x+1)
How do I write the code below in a list comprehension style?
Residual = np.zeros((noRows, noRows))
Dist = np.zeros((noRows, noRows))
for i in range(noRows):
for j in range(noRows):
Residual[i][j] = (data[data.columns[2]][i]-data[data.columns[2]][j])**2
Dist[i][j] = (data[data.columns[0]][i]-data[data.columns[1]][j])**2
We don't have full code, but from what I can see from your snippet, solution should be something like this:
Residual = [(data[data.columns[2]][i]-data[data.columns[2]][j])**2 for i in range(noRows) for j in range(noRows)]
Dist = [(data[data.columns[0]][i]-data[data.columns[1]][j])**2 for i in range(noRows) for j in range(noRows)]
For example:
noRows = 5
l = [i+j for i in range(noRows) for j in range(noRows)]
print(l)
Output will be:
[0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8]
Press any key to continue . . .
In your case the statement will be way more complicated compared to "i+j" operation.... but list comprehension should be right.