Contract m-repeated numbers in list to n-repeated (n<m) in place in O(1) space - python-3.7

I want to write a python 3.7 function that has a sorted list of numbers as an input, and a number n which is the max number each one of the integers can be repeated and modifies the list in place, so that any numbers that are repeated more than n times, would be cut to n repeats, and it should be done in O(1) space, no additional data structures allowed (e.g. set()). Special case - remove duplicates where n = 1. Example:
dup_list = [1, 1, 1, 2, 3, 7, 7, 7, 7, 12]
dedup(dup_list, n = 1)
print(dup_list)
[1, 2, 3, 7, 12]
dup_list = [1, 1, 1, 2, 3, 7, 7, 7, 7, 12]
dedup(dup_list, n = 2)
print(dup_list)
[1, 1, 2, 3, 7, 7, 12]
dup_list = [1, 1, 1, 2, 3, 7, 7, 7, 7, 12]
dedup(dup_list, n = 3)
print(dup_list)
[1, 1, 1, 2, 3, 7, 7, 7, 12]
Case n = 1 is easy, the code is below (code is taken from Elements of Prograqmming Interviews, 2008, page 49 except the last line return dup_list[:write_index]):
def dedup(dup_list):
if not dup_list:
return 0
write_index = 1
for i in range(1, len(dup_list)):
if dup_list[write_index-1] != dup_list[i]:
dup_list[write_index] = dup_list[i]
write_index += 1
return dup_list[:write_index]

This should work:
def dedup2(dup_list, n):
count = 1
list_len = len(dup_list)
i = 1
while i < list_len:
if dup_list[i - 1] != dup_list[i]:
count = 1
else:
count += 1
if count > n:
del(dup_list[i])
i -= 1
list_len -= 1
i += 1
return dup_list
print(dedup2([1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 8, 9], 1))

Related

array_positions() with comparison, not equality

Postgres has the function array_positions:
array_positions(ARRAY[1, 4, 3, 1, 3, 4, 2, 1], 1);
-> {1,4,8}
Which checks for equality of the second argument. I can't find anywhere if it has a function for comparison. E.g. (element in array > 1) which would return {2, 3, 5, 6, 7} in this example.
Does postgres have an efficient way of doing this?
SELECT position
FROM UNNEST(ARRAY[1, 4, 3, 1, 3, 4, 2, 1]) WITH ORDINALITY a(element, position)
WHERE element > 1

Sorting with higher order functions: Giving precedence to one element

With an unordered array of Ints as such:
let numbers = [4, 3, 1, 5, 2]
Is it possible in Swift, using .sorted { }, to order the array with one item prioritised and placed in the first index of the array. So instead of returning [1, 2, 3, 4, 5] we could get [3, 1, 2, 4, 5]?
You can declare a function like this :
func sort(_ array: [Int], prioritizing n: Int) -> [Int] {
var copy = array
let pivot = copy.partition { $0 != n }
copy[pivot...].sort()
return copy
}
Which uses the partition(by:) function.
You could use it like so:
let numbers = [4, 3, 1, 5, 2]
let specialNumber = 3
sort(numbers, prioritizing: specialNumber) //[3, 1, 2, 4, 5]
Here are some test cases :
sort([3, 3, 3], prioritizing: 3) //[3, 3, 3]
sort([9, 4, 1, 5, 2], prioritizing: 3) //[1, 2, 4, 5, 9]
Here an alternative solution that uses sorted(by:) only :
let numbers = [4, 3, 1, 5, 2]
let vipNumber = 3
let result = numbers.sorted {
($0 == vipNumber ? Int.min : $0) < ($1 == vipNumber ? Int.min : $1)
}
print(result) //[3, 1, 2, 4, 5]

Do you have any idea or documentation about why we have arc4random_stir() in swift?

I have written the program below for generating random unique numbers for several number of times by invoking the function, but it seems like I'm getting the same pattern with minimal changes.
func generateRandom(withinNumber: Int) {
var i:Int = 0
var elements = Set<Int>()
while i != withinNumber {
let num:Int = Int(arc4random())%withinNumber + 1
if elements.count <= withinNumber && elements.contains(num) == false {
elements.insert(num)
}
else {
i = i-1
}
i=i+1
}
print(elements)
elements.removeAll()
}
generateRandom(withinNumber: 10)
How does I make my program effectively run to generate several random unique numbers.
Please let me know it would be very helpful for me.
You are storing your numbers in a Set and sets are not ordered, so the order the elements are shown by print is unrelated to the order in which they were added to the set.
Rather the elements of a set are stored in some manner which enables fast checking for .contains(), and this is one reason you seeing similar sequences.
If you wish to preserve order of insertion use a collection which does this, i.e. an array. Changing to an array in your code produced the following results from 9 calls:
[8, 9, 7, 10, 5, 6, 2, 3, 1, 4]
[4, 9, 10, 3, 6, 2, 1, 7, 8, 5]
[8, 3, 5, 1, 6, 4, 9, 10, 7, 2]
[5, 7, 2, 9, 8, 1, 6, 10, 3, 4]
[2, 3, 7, 6, 9, 1, 8, 10, 5, 4]
[9, 10, 2, 4, 6, 8, 5, 7, 1, 3]
[9, 10, 2, 5, 4, 7, 3, 8, 1, 6]
[1, 6, 4, 5, 8, 2, 3, 9, 7, 10]
[6, 10, 5, 3, 2, 8, 1, 9, 7, 4]
You are also generating 10 random numbers in the range 1 to 10 and avoiding duplicates, so the results is always going to be the numbers 1 to 10 in some order.
To generate a random number in a given range do not use %, instead use the provided arc4random_uniform() which will give better a better distribution.
The function mention in your title arc4random_stir() is available in Swift.
BTW (somewhat opinion based): It is better to write !e (! being the boolean not operator) rather than e == false, and never ever write e == true which is the long form of e!
BTW (SO etiquette): Don't link to your code (or paste in images of it). Reduce to a small example which demonstrates the issue (not required in your case) and insert directly in the question. Keep tags minimal and appropriate. These edits were done for you this time by myself and others, you will know for next time.
HTH

illegal division by zero:Perl

I have written a code to find determinant of a 10X10 matrix. This code gives the proper result till 9X9 matrix. But for 10X10 matrix gives the following error
"Use of Uninitialized value in multiplication <*> at line 23
Illegal division by zero at line 21"
I tried for 11X11 matrix also, but it is giving the wrong answer.
Why this code is giving such error...
Following is the code:
#!/usr/bin/perl
use strict;
use warnings;
my #x1=(
[5, 6, 3, 2, 4, 9, 3, 5, 4, 2],
[12, 9, 8, 3, 3, 0, 6, 9, 3, 4],
[8, 6, 5, 8, 9, 3, 9, 3, 9, 5],
[6, 4, 3, 0, 6, 4, 8, 2, 22, 8],
[8, 3, 2, 5, 2, 12, 7, 1, 6, 9],
[5, 9, 3, 9, 5, 1, 3, 8, 4, 2],
[3, 10, 4, 16, 4, 7, 2, 12, 9, 6],
[2, 12, 9, 13, 8, 3, 1, 16, 0, 6],
[3, 6, 8, 5, 12, 8, 4, 19, 8, 5],
[2, 5, 6, 4, 9, 10, 3, 11, 7, 3]
);
# Matrix of nxn
for (my $i=0;$i le 9;$i++) {
for (my $j=0;$j le 9;$j++) {
if($j>$i) {
my $ratio = $x1[$j][$i]/$x1[$i][$i];
for(my $k = 0; $k le 9; $k++){
$x1[$j][$k] -= $ratio * $x1[$i][$k];
}
}
}
}
my $det1 = 1;
for(my $i = 0; $i le 9; $i++){
$det1 *= $x1[$i][$i];
}
printf $det1," ";
le doesn't do what you think it does. http://perldoc.perl.org/perlop.html
Binary "le" returns true if the left argument is stringwise less than or equal to the right argument.
print 10 le 9,"\n";
print 10 <= 9,"\n";
It's a stringwise comparison not a numeric one.
So "10" le "9" is true, because alphabetically 10 is before 9.
But this would work fine for a smaller matrix, because 9 le 8 is a valid comparison and works the 'right way'.
You should use <= instead:
Binary "<=" returns true if the left argument is numerically less than or equal to the right argument.
You can also probably auto-scale by using $#x1 for your comparison, which is the value of the last array index. In your example above, $#x1 is 9, because your array is 0-9

Object range with conditions

In groovy I can write
def n = 10
print 1..<n
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Are there other language that allow to specify range with conditions?
examples
def n = 10
print 1<=..n
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def n = -2
print 1<=..n
Output: [1]
def n = -2
print 1..n
Output: [1, 0, -1, -2]
Python has the range() method which does a similar thing. While it does not use operators for the condition you can specify a start value, stop value and step value. It then creates a list containing all values starting with the start value, then start+step, ... until it reaches the end value (which is not included).