Best Logic to implement comparison of two arrays
I have two arrays arr 1 and arr 2, here both the arrays will have some common items and many uncommon items, first the common items should be removed from both the arrays.
therefore for each uncommon item in arr 1 may probably be a sum of two or more values in arr 2 or vice versa.if the sum is found the values must be removed from the respective arrays. Finally the output should only be the unmatched values on both the arrays
I need a logic where i can do this calculation in much faster way.
1 answer

I'm not going to give out the code that implements your logic but rather I would be happy to point you in the right direction.
I code in C++, so I'm gonna answer with respect to that. If you want a different language, I'm sure you can freely do so.
To remove the common elements:
You first sort the arrays
arr1
andarr2
. Then do a set_symmetric_difference on them. This will effectively run in linear time. Then create two sets out of them, sayset1
andset2
.To remove pairs that sum up to an element in the other array
For this, you need to loop through all the possible pairs of elements in
arr1
and check if this sum of this pair exists inset2
. Likewise do forarr2
as well and remove the element when necessary.This can be done in O(n^{2}). If you don't want to create the extra sets, you can always trade performance for memory, by just looping through the pairs of
arr1
and checking for the sum in thearr2
by doing a binary search. Then the time complexity may shoot up to O(n^{2} log(n)).
See also questions close to this topic

Craps Game  Few Errors
So i am creating this craps game, which im really close to finishing(or so i think). i know it looks sloppy and i plan on factoring it into smaller concise code. But i am running into a couple issues and i can't do that until the logic is set. Hopefully someone can help me here. I will make a tally of the issues im running into.
1.) It will print the output of rolls for both players, for example
9
9
3
3
4
4
7 rolled, you lose.
7
7 rolled, you lose.
7
{'n': 80, 't': 80}
this is the output i receive. As you can see it shows the output of both players. Trying to get just one output for all these youll notice that it will out double since there are two players, triple for 3 etc. I tried using break statements and that looked ugly.
2.) It will do weird calculations at times like apply the right amount of payout odds to the first player but the wrong amount to the second player, for example
Point established, point is: 8
{'nick': 90, 'bill': 90}
Do you want to bet pass or no(y/n): y
Do you want to bet pass or no(y/n): y
How much would you like to bet: 10
How much would you like to bet: 10
8 is equal to 8, you win.
100.0
{'nick': 100.0, 'bill': 80}
{'nick': 100.0, 'bill': 80}
Again weird logic issue. Maybe someone can spot it? Ive been working on this code for a long time and really struggling.
4.) I will do classes eventually but for now i just want to get the logic right in functions and get it working like this before i move on to classes.
Hopefully someone can help I am more than open to criticism but don't be egregious where you make me feel dumb for beginner mistakes since i have written all this code completely from scratch myself. Still some things i need to work out so if you happen to notice something i appreciate the help! Cheers!
from random import choice,randint
#no need to make side bets. keep it simple # i will need to have this variable global so it doesn't reset, than once the maximum length # of rollers is reached. reset and have a new set of rollers go again player_rolled = [] shooters = [] # bet = int(input("Place a bet: ")) crap_out = [2, 3, 12] comeout_win = [7, 11] # simple bets double_bet = (2/1) # complex bets # thr_elv_odds = (16/1) # sev_pay = (5/1) # four_and_ten_pay = (2/1) # five_nine_pay = (3/2) # six__et_pay = (6/5) # craps_pay = (8/1) # trying different ways to incorporate bets payouts = { 2: (8/1), 3: (8/1), 4: (2/1), 5: (3/2), 6: (6/5), 7: (5/1), 8: (6/5), 9: (3/2), 10: (2/1) } players_and_bets= {} amt = 100 point = 0 def roll_dice(): dice1 = range(1,7) dice2 = range(1,7) roll = (choice(dice1) + choice(dice2)) return roll def get_info(): done = False count = 0 global amt players_in_game = int(input("How many people would like to play: ")) while done == False: if count < players_in_game: name = input("Enter a simple name to play: ").lower() buy_in = int(input(f"The amount to buy_in is {amt}, enter the amount to continue: ")) players_and_bets[name] = buy_in count += 1 else: print("Table at capacity") done = True def players_bets(): bets = [] for player, amt in players_and_bets.items(): while True: try: bet = int(input("How much would you like to bet: ")) amt = bet players_and_bets[player] = amt bets.append(bet) break except ValueError: print("That is not a number, try again.") return bets def point_bets(): bets = [] for player, amt in players_and_bets.items(): while True: try: bet = int(input("How much would you like to bet: ")) amt = bet players_and_bets[player] = amt bets.append(bet) break except ValueError: print("That is not a number, try again.") return bets def players_pass_bets(): pass_list = [] no_pass = [] for player, amt in players_and_bets.items(): pass_bet = input("Do you want to bet pass or no(y/n): ").lower() if pass_bet == "y": pass_list.append(player) else: no_pass.append(player) return pass_list, no_pass def players_turn(): global shooters for player in players_and_bets.keys(): shooters.append(player) def come_out(): bets = players_bets() pass_list, no_pass = players_pass_bets() roll = roll_dice() done = False players_turn_arr = players_turn() global point # for player in players.keys(): # shooters.append(player) for player, amt in players_and_bets.items(): for bet in bets: if roll in crap_out: print(f"{player} rolled: {roll}") if player in no_pass: amt += (bet*double_bet) players_and_bets[player] = amt elif roll in comeout_win: print(f"{player} rolled: {roll}") if player in pass_list: amt += (bet*double_bet) players_and_bets[player] = amt else: print(f"Point established, point is: {roll}") point = roll # print(players_and_bets) point_round() break break def point_round(): global point done = False dice = roll_dice() pass_list, no_pass = players_pass_bets() bet_amts = point_bets() while done == False: dice = roll_dice() for player, amt in players_and_bets.items(): for payout, value in payouts.items(): for bet in bet_amts: if dice == 7: print(f"{dice} rolled, you lose.") done = True break if dice == point and point == value: print(f"{dice} is equal to {point}, you win.") # if point == payout and player in pass_list: amt += (bet*value) # print(amt) players_and_bets[player] = amt # print(players_and_bets) done = True break else: print(dice) continue break break break # first_run=False def main(): start = get_info() done = False while done == False: play_game = come_out() print(players_and_bets) for player in players_and_bets: replay = input("Would you like to keep playing(y/n): ").lower() if replay == "y": continue else: del player print(players_and_bets) print("Thank you for playing craps, take care") done = True main()
Use this link to view code since its very long.
EDIT: https://github.com/NicholasRotondo/craps/blob/master/craps.py

Arguments are not sufficiently instantiated prolog
Trying to write a program that solves this problem:
"You are given two jugs, a 5gallon one and a 3gallon one. Neither of them has any measuring markers on it. There is a tap that can be used to fill the jugs with water. The goal is to obtain exactly 4 gallons of water." This is the code I currently have:
%(WaterInThree, WaterInFive) s(A,B). initial(s(0,0)). goal(s(A,B)): A + B =:= 4. valid(A,B) : A =< 3, A >=0, B =< 5, B >= 0. changeState(s(0,B),s(3,B)). changeState(s(A,0),s(A,5)). changeState(s(A,B),s(A,0)) : B > 0, valid(A,B). changeState(s(A,B),s(0,B)) : A > 0, valid(A,B). changeState(s(A,B),s(C,D)) : B =< 2, D is A + B, C is B  A, valid(C,D). changeState(s(A,B),s(C,D)) : B > 2, D is B + ( (5  A) mod 5), valid(C,D). changeState(s(A,B),s(3,D)) : B =< 3, D is B  (3  A), valid(A,D). changeState(s(A,B),s(C,D)) : B =:= 5, C is A + B, D is A  B, valid(C,D). traverse(StartNode,Sol,_) : goal(StartNode), Sol = [StartNode]. traverse(StartNode,Sol,Visit) : changeState(StartNode, NextNode), not(member(NextNode, Visit)), traverse(NextNode, PartialSol, [NextNodeVisit]), Sol = [StartNode  PartialSol].
When I execute this code, I get these errors:
ERROR: Arguments are not sufficiently instantiated ERROR: In: ERROR: [13] _11226=<3 ERROR: [12] valid(_11252,5+(53)) ERROR: [11] changeState(s(3,5),s(_11302,5+ ...)) ERROR: [10] traverse(s(3,5),_11336,[s(3,5),......]) ERROR: [9] traverse(s(3,0),_11388,[s(3,0),...]) ERROR: [8] traverse(s(0,0),_11440,[s(0,0)]) ERROR: [7] <user>
Not really sure what the issue is, I've been trying to fix this for hours on end and can't figure out what's wrong. Can't even properly test if the changeState predicates I've created even work because of this so it's extremely frustrating. Would appreciate any help!
EDIT I've narrowed it down to the this line causing the error:
changeState(s(A,B),s(C,D)) : B > 2, D is B + ( (5  A) mod 5), valid(C,D).
Can't really see how this would cause that error though.

A program for finding a weekday of a date by a given month,day and the 1st day of the given month
Example for the output:
>Hello! Welcome to the day calculator Enter a month to check: (1Jan, 2Feb, etc) 2 >Enter day to check: 21 >Enter the weekday of the 1st of the month(1Sunday, 2Monday, etc) 5 >The 21.2 will be a Wednesday
Is there any formula I can to use to shorten this code?

Having a table of elements equality , calculate number of "unique" elements
I have two tables: table of product ids:
table p_test:
id: 1 2 3 4
and a table of all possible combinations of a product tested with every other product for equality (let't say I test their pictures and if the pictures are equal  I assume they are same product): table p_match_test:
p_id_1, p_id_2, is_same 1 2 0 1 3 0 1 4 1 2 3 1 2 4 0 3 4 0
as you can see in this example there is total of 4 products. Product 1 was tested for equality with product 4 and they were found "same", same thing about products 2 and 3. This means that there are only two "unique" products (2 unique pictures for 4 products).
What I want to achieve is to have a count of unique pictures in any group of products so the result will be:
products_count unique_products_count 4 2
I tried this query:
select count(p.id) as products_count, sum(if(not exists(select * from p_match_test t where (p.id = t.p_id_1 or p.id = t.p_id_2) and t.is_same = 1), 1, 0)) as unique_products_count from p_test p
but it counts only products that have no duplicates instead of counting "unique" images. How do I achieve my goal?

Find every possible disposition of elements in a list keeping elements and list length
I would like to find every combination of elements in a list, by switching the order of the elements, while keeping their count and the length of the list.
For example, for the list:
[1,2,1,3]
I would like to obtain the following:
[1,1,2,3] [1,1,3,2] [1,2,1,3] [1,2,3,1] [1,3,1,2] [1,3,2,1] [2,1,1,3] [2,1,3,1] [2,3,1,1] [3,1,1,2] [3,1,2,1] [3,2,1,1]
Be aware that same elements, such as the two 1 in the original list, are not seen as separate ones, so the two possibilities
[1a,1b,2,3]
and[1b,1a,2,3]
must be counted as one 
Calculate possible combinations of banknotes in Java
I have this question: how can I calculate all the possible combinations of banknotes in Java given a number of banknotes? The output should be like this:
You can only use bills of 20€, 50€ and 100€.
200€ = 10 * 20€
200€ = 5 * 20€ + 2 * 50€
200€ = 2 * 100€
...
I cannot think about a solution for this. Thanks.

Get all combinations of n elements taken consecutively from the start of m arrays (in Java)
My problem is kind of hard to describe, so I will try to demonstrate it on an example.
Taken from the title, let n = m = 3. Lets suppose you have these 3 arrays:
 [1, 2, 3, 4]
 [10, 20, 30, 40]
 [5, 6, 7, 8, 9]
I want all combinations with n (= 3) numbers, where the numbers are always chosen consecutively from the start of an array.
That would be: [1, 2, 3], [1, 2, 10], [1, 2, 5], [1, 10, 20], [1, 10, 5], [1, 5, 6], [10, 20, 30], ..., [5, 6, 7].
I'm not quite sure how that can be implemented and would be grateful for any advice!

Write a loop to select all combination of variable values generating positive equation values in R
I have the following four equations (a,b,c,d), with several different variables (x,t,v,w,n,f). My goal would be to try and find all sets of variable values that would generate all positive (and nonzero) numbers for equations(a,b,c,d). A regular loop would just go through each number of the sequence generated and systematically check if it generates a positive value or not. I want it to pick up random numbers from each sequence and test it against the others in R. For example (x=8, t = 2.1,v=13,w=1,n=10,f=1) is a possible set of combinations.
Please do not suggest analytically solving for these and then finding out values. These are simply representations of equations I'm dealing with. The equations I have are quite complex, and more than 15 variables.
#Equations a < x * t  2*x b < v  x^2 c < x  w*t  t*t d < (n  f)/t x < seq(from = 0.0001, to = 1000, by = 0.1) t < seq(from = 0.0001, to = 1000, by = 0.1) v < seq(from = 0.0001, to = 1000, by = 0.1) w < seq(from = 0.0001, to = 1000, by = 0.1) n < seq(from = 0.0001, to = 1000, by = 0.1) f < seq(from = 0.0001, to = 1000, by = 0.1)

Combinations vs Permutations
Not knowing the terminology to use, I am trying to achieve the following in python:
pairs = [perm for perm in permutations( range( 7 ), 2)]
This gives me the following:
pairs = [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 0), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 0), (2, 1), (2, 3), (2, 4), (2, 5), (2, 6), (3, 0), (3, 1), (3, 2), (3, 4), (3, 5), (3, 6), (4, 0), (4, 1), (4, 2), (4, 3), (4, 5), (4, 6), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 6), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5)]
Following Constraints: 1. Each pair only appears once in unique_groups 2. Pair are placed in groups such that the numbers (0  6) only appear one in the whole group of pairs
Example [[(1, 2), (3, 4), (5, 6)], [(2, 1), (4, 3), (6, 5)] = Correct Example [(1, 2), (3, 4), (5, 1)], [(2, 1), (3, 4), (6, 5)]] = Incorrect 1 appears twice in the entry and pair (3, 4) is used twice in the set. unique_groups = do_something_cool(pairs) unique_groups = [ [(0, 1), (2, 3), (4, 5)], [(0, 2), (1, 3), (4, 6)], [(0, 3), (1, 2), (5, 4)], [(0, 4), (1, 5), (2, 6)], [(0, 5), (1, 4), (2, 5)], [(0, 6), (2, 1), (3, 4)], [(1, 0), (2, 4), (3, 5)], [(1, 6), (2, 0), (4, 3)], [(3, 0), (4, 1), (5, 2)], [(3, 1), (4, 0), (5, 3)], [(3, 2), (5, 0), (6, 1)], [(3, 6), (4, 2), (5, 1)], [(5, 6)], [(6, 0)], [(6, 2)], [(6, 3)], [(6, 4)], [(6, 5)], ]
There might be multiple answers to this question. I am looking for something in the either the python libraries that does this or some simple code that does it.
Thanks for any help.
Update: The following code produces the right output:
pairs = [perm for perm in permutations( range(7), 2)] list_o = [] while len(pairs) > 0: pair = pairs.pop(0) inserted = False for item in list_o: found = False for item2 in item: if pair[0] in item2 or pair[1] in item2: found = True break if not found: item.append(pair) inserted = True break if not inserted: list_o.append([pair]) print(list_o) [ [(0, 1), (2, 3), (4, 5)], [(0, 2), (1, 3), (4, 6)], [(0, 3), (1, 2), (5, 4)], [(0, 4), (1, 5), (2, 6)], [(0, 5), (1, 4), (3, 2)], [(0, 6), (2, 1), (3, 4)], [(1, 0), (2, 4), (3, 5)], [(1, 6), (2, 0), (4, 3)], [(2, 5), (3, 0), (4, 1)], [(3, 1), (4, 0), (5, 2)], [(3, 6), (4, 2), (5, 0)], [(5, 1), (6, 0)], [(5, 3), (6, 1)], [(5, 6)], [(6, 2)], [(6, 3)], [(6, 4)], [(6, 5)] ]
P.S. I am horrible at python is there a better way to do this? Also is there a way to have all entries be 3 pairs and not the double or single pair at the bottom?