Related
So this is a variation of the Knapsack Problem I came with the other day.
It is like a 0-1 Knapsack Problem where there are multiple groups and each item belongs to only one group. The goal is to maximize the profits subject to the constraints. In this case, a fixed number of items from each group have to be chosen for each group.
It is similar to the Multiple Choice Knapsack Problem, but in that case you only pick 1 of item of each group, in this one you want to pick x amount of items of each group
So, each item has: value, weight and group
Each group has an item count (Ex: if group A (or 0) has 2, the final solution needs to have 2 items of group A, no more no less)
And and you also have a maximum capacity (not related to the groups)
This translates into:
values[i] = The value of the ith element
weights[i] = The weigth of the ith element
groups[i] = The group of the ith element
C = Capacity
n = Amount of elements
m = Amount of groups
count[j] = Amount of items of group j
I'm attempting a Recursive solution first and then I will try a Dynamic approach.
Any solution would be appreciated (preferably Python, but anything will do :) ).
Usefull links I found:
Theorical solution of a similar problem
First approach to the Multiple Choice Knapsack Problem
Multiple Choice Knapsack Problem solved in Python
Knapsack with count constraint
Full code also in: https://github.com/pabloroldan98/knapsack-football-formations
Explanation after the code.
This code is for an example where you have a Fantasy League with a playersDB where each player has price (weight), points (value) and position (group); there is a list of possible_formations (group variations); and a budget (W) you can't go over.
Full code:
main.py:
from group_knapsack import best_full_teams
playersDB = [
Player(name="Keylor Navas", price=16, points=7.5, position="GK"),
Player(name="Laporte", price=23, points=7.2, position="DEF"),
Player(name="Modric", price=22, points=7.3, position="MID"),
Player(name="Messi", price=51, points=8.2, position="ATT"),
...
]
possible_formations = [
[3, 4, 3],
[3, 5, 2],
[4, 3, 3],
[4, 4, 2],
[4, 5, 1],
[5, 3, 2],
[5, 4, 1],
]
budget = 300
best_full_teams(playersDB, possible_formations, budget)
group_knapsack.py:
import itertools
from MCKP import knapsack_multichoice_onepick
def best_full_teams(players_list, formations, budget):
formation_score_players = []
for formation in formations:
players_points, players_prices, players_comb_indexes = players_preproc(
players_list, formation)
score, comb_result_indexes = knapsack_multichoice_onepick(
players_prices, players_points, budget)
result_indexes = []
for comb_index in comb_result_indexes:
for winning_i in players_comb_indexes[comb_index[0]][comb_index[1]]:
result_indexes.append(winning_i)
result_players = []
for res_index in result_indexes:
result_players.append(players_list[res_index])
formation_score_players.append((formation, score, result_players))
print("With formation " + str(formation) + ": " + str(score))
for best_player in result_players:
print(best_player)
print()
print()
formation_score_players_by_score = sorted(formation_score_players,
key=lambda tup: tup[1],
reverse=True)
for final_formation_score in formation_score_players_by_score:
print((final_formation_score[0], final_formation_score[1]))
return formation_score_players
def players_preproc(players_list, formation):
max_gk = 1
max_def = formation[0]
max_mid = formation[1]
max_att = formation[2]
gk_values, gk_weights, gk_indexes = generate_group(players_list, "GK")
gk_comb_values, gk_comb_weights, gk_comb_indexes = group_preproc(gk_values,
gk_weights,
gk_indexes,
max_gk)
def_values, def_weights, def_indexes = generate_group(players_list, "DEF")
def_comb_values, def_comb_weights, def_comb_indexes = group_preproc(
def_values, def_weights, def_indexes, max_def)
mid_values, mid_weights, mid_indexes = generate_group(players_list, "MID")
mid_comb_values, mid_comb_weights, mid_comb_indexes = group_preproc(
mid_values, mid_weights, mid_indexes, max_mid)
att_values, att_weights, att_indexes = generate_group(players_list, "ATT")
att_comb_values, att_comb_weights, att_comb_indexes = group_preproc(
att_values, att_weights, att_indexes, max_att)
result_comb_values = [gk_comb_values, def_comb_values, mid_comb_values,
att_comb_values]
result_comb_weights = [gk_comb_weights, def_comb_weights, mid_comb_weights,
att_comb_weights]
result_comb_indexes = [gk_comb_indexes, def_comb_indexes, mid_comb_indexes,
att_comb_indexes]
return result_comb_values, result_comb_weights, result_comb_indexes
def generate_group(full_list, group):
group_values = []
group_weights = []
group_indexes = []
for i, item in enumerate(full_list):
if item.position == group:
group_values.append(item.points)
group_weights.append(item.price)
group_indexes.append(i)
return group_values, group_weights, group_indexes
def group_preproc(group_values, group_weights, initial_indexes, r):
comb_values = list(itertools.combinations(group_values, r))
comb_weights = list(itertools.combinations(group_weights, r))
comb_indexes = list(itertools.combinations(initial_indexes, r))
group_comb_values = []
for value_combinations in comb_values:
values_added = sum(list(value_combinations))
group_comb_values.append(values_added)
group_comb_weights = []
for weight_combinations in comb_weights:
weights_added = sum(list(weight_combinations))
group_comb_weights.append(weights_added)
return group_comb_values, group_comb_weights, comb_indexes
MCKP.py:
import copy
def knapsack_multichoice_onepick(weights, values, max_weight):
if len(weights) == 0:
return 0
last_array = [-1 for _ in range(max_weight + 1)]
last_path = [[] for _ in range(max_weight + 1)]
for i in range(len(weights[0])):
if weights[0][i] < max_weight:
if last_array[weights[0][i]] < values[0][i]:
last_array[weights[0][i]] = values[0][i]
last_path[weights[0][i]] = [(0, i)]
for i in range(1, len(weights)):
current_array = [-1 for _ in range(max_weight + 1)]
current_path = [[] for _ in range(max_weight + 1)]
for j in range(len(weights[i])):
for k in range(weights[i][j], max_weight + 1):
if last_array[k - weights[i][j]] > 0:
if current_array[k] < last_array[k - weights[i][j]] + \
values[i][j]:
current_array[k] = last_array[k - weights[i][j]] + \
values[i][j]
current_path[k] = copy.deepcopy(
last_path[k - weights[i][j]])
current_path[k].append((i, j))
last_array = current_array
last_path = current_path
solution, index_path = get_onepick_solution(last_array, last_path)
return solution, index_path
def get_onepick_solution(scores, paths):
scores_paths = list(zip(scores, paths))
scores_paths_by_score = sorted(scores_paths, key=lambda tup: tup[0],
reverse=True)
return scores_paths_by_score[0][0], scores_paths_by_score[0][1]
player.py:
class Player:
def __init__(
self,
name: str,
price: float,
points: float,
position: str
):
self.name = name
self.price = price
self.points = points
self.position = position
def __str__(self):
return f"({self.name}, {self.price}, {self.points}, {self.position})"
#property
def position(self):
return self._position
#position.setter
def position(self, pos):
if pos not in ["GK", "DEF", "MID", "ATT"]:
raise ValueError("Sorry, that's not a valid position")
self._position = pos
def get_group(self):
if self.position == "GK":
group = 0
elif self.position == "DEF":
group = 1
elif self.position == "MID":
group = 2
else:
group = 3
return group
Explanation:
Okay,so I managed to find a solution translating what was here: Solving the Multiple Choice Knapsack Problem from C++ to Python. My solution also gives the path that got you to that solution. It uses Dynamic Programming and it's very fast.
The input data, instead of having groups[i], has the weights and the values as a list of lists, where every list inside represent the values of each group:
weights[i] = [weights_group_0, weights_group_1, ...]
values[i] = [values_group_0, values_group_1, ...]
Where:
weights_group_i[j] = The weigth of the jth element of the ith group
values_group_i[j] = The value of the jth element of the ith group
Those would be the inputs of knapsack_multichoice_onepick. Here is an example:
# Example
values = [[6, 10], [12, 2], [2, 3]]
weights = [[1, 2], [6, 2], [3, 2]]
W = 7
print(knapsack_multichoice_onepick(weights, values, W)) # (15, [(0, 1), (1, 1), (2, 1)])
After that I followed #user3386109 's suggestion and did the combinations with the indexes. The group preprocesing methods are players_preproc, generate_group and group_preproc.
Again, this code is for an example where you have a Fantasy League with a playersDB where each player has price (weight), points (value) and position (group); there is a list of possible_formations (group variations); and a budget (W) you can't go over.
The best_full_teams method prints everything and uses all the previous ones.
One of the Rugby Coaches at my school have asked me to code a conditional rugby match draw for the upcoming games with the task laid out something like this: Given a list of teams from 1 - 12 split into 3 groups ([Group1 = 1, 2, 3, 4], [Group2 = 5, 6, 7, 8,], [Group3 = 9, 10, 11, 12])
generate and print an 11 round-robin matchup with the conditions that:
Teams in Group1 does NOT verse teams in Group3
Teams in Group1 verses every other team in Group 1 twice (Eg. 1v2, 2v1, 1v3, 3v1, 1v4, 4v1, 1v5, 5v1.....)
This same rule applies to teams in Group3 as they verse other teams in Group3
Teams in Group2 verse every other team once.
Teams in Group1 and Group3 need one Bye Game.
I have attempted multiple times but inevitably become stuck, below are my 2 attempts:
Attempt 1:
import operator
import functools
import random
###First Generation (Flawed unclean round robin)
def fixtures(teams):
if len(teams) % 2:
teams.append('Day off') # if team number is odd - use 'day off' as fake team
rotation = list(teams) # copy the list
random.shuffle(rotation)
fixtures = []
for i in range(0, len(teams)-1):
fixtures.append(rotation)
rotation = [rotation[0]] + [rotation[-1]] + rotation[1:-1]
return fixtures
def main():
# demo code
teams = ["Team1","Team2","Team3","Team4","Team5","Team6","Team7","Team8","Team9","Team10","Team11","Team12"]
groupA = ["Team1","Team2","Team3","Team4"]
groupB = ["Team5","Team6","Team7","Team8"]
groupC = ["Team9","Team10","Team11","Team12"]
# for one match each - use this block only
matches = fixtures(teams)
print("flawed matches:")
RoundCounter = 0
homeTeams = []
awayTeams = []
for f in matches:
#print(f)
homeTeams = f[::2]
awayTeams = f[1::2]
print("Home Teams:{}".format(homeTeams))
print("Away Teams:{}".format(awayTeams))
HomeTeamGroupA = set(homeTeams).intersection(groupA)
HomeTeamGroupC = set(homeTeams).intersection(groupC)
AwayTeamGroupA = set(awayTeams).intersection(groupA)
AwayTeamGroupC = set(awayTeams).intersection(groupC)
VSCounter = 0
for p, o in zip(homeTeams, awayTeams):
if p in HomeTeamGroupA:
if o in AwayTeamGroupC:
AvsCPosition = awayTeams.index(o)
VSCounter += 1
RoundCleanUp(homeTeams, awayTeams, AvsCPosition, VSCounter) #if this is returned begin cleaning the round
else: print("GroupA is versing either Group B or GroupA") #if this is returned it is a team 1-4 but is vs either group b or group a
elif p in HomeTeamGroupC:
if o in AwayTeamGroupA:
AvsCPosition = awayTeams.index(o)
VSCounter += 1
RoundCleanUp(homeTeams, awayTeams, AvsCPosition, VSCounter) #if this is returned begin cleaning the round
else:
print("GroupC is versing either Group B or GroupC") #if this is returned it is a team 9-12 but is vs either group b or group c
else:
pass
def RoundCleanUp(HTeam, ATeam, AvsCPos, VSCounter):
##gets Value of List at position
HTeamVal = HTeam[AvsCPos]
ATeamVal = ATeam[AvsCPos]
main()
Attempt 2:
import operator
import functools
import random
def make_round(rotation, num_teams, fixtures):
for i in range(num_teams - 1):
rotation = list(range(1, num_teams + 1))
# clip to 0 .. num_teams - 2 # if i == 0, no rotation is needed (and using -0 as list index will cause problems)
i %= (num_teams - 1)
if i:
rotation = rotation[:1] + rotation[-i:] + rotation[1:-i]
half = num_teams // 2
fixtures.append(list(rotation[:half]))
fixtures.append(list(rotation[half:][::-1]))
return fixtures
def make_schedule(teams):
"""Produces RoundRobin"""
# number of teams must be even
TeamLength = len(teams)
if TeamLength % 2:
TeamLength += 1 # add a dummy team for padding
# build first round-robin
rotation = list(teams)
Fixture = []
schedule = make_round(rotation, TeamLength, Fixture)
return schedule
def homeAwayRotation(matches):
for homeTeams, awayTeams in zip(matches[0::2], matches[1::2]):
print("Home Rotation: {}".format(homeTeams))
print("Away Rotation: {}".format(awayTeams))
validation(homeTeams, awayTeams)
def validation(homeTeams, awayTeams):
groupA = [1, 2, 3, 4]
groupC = [9, 10, 11, 12]
for x, y in zip(homeTeams, awayTeams):
if x in groupA:
if y in groupC:
AvsCPosition = awayTeams.index(y)
cleanDirtyData(homeTeams, awayTeams, AvsCPosition)
else:
# if this is returned it is a team 1-4 but is vs either group b or group a
print("Group A vsing either itself or GroupB\n")
elif x in groupC:
if y in groupA:
AvsCPosition = awayTeams.index(y)
cleanDirtyData(homeTeams, awayTeams, AvsCPosition)
else:
# if this is returned it is a team 9-12 but is vs either group b or group c
print("Group C vsing either itself or GroupB\n")
else:
# if this is returned it is a team in group B
print("This is team B\n")
def cleanDirtyData(homeTeams, awayTeams, AvsCPosition):
HTeamVal = homeTeams[AvsCPosition]
ATeamVal = awayTeams[AvsCPosition]
Dirtlist = []
Dirtlist.append(HTeamVal)
Dirtlist.append(ATeamVal)
def main():
# demo code
teams = ["Team1", "Team2", "Team3", "Team4", "Team5", "Team6",
"Team7", "Team8", "Team9", "Team10", "Team11", "Team12"]
# for one match each - use this block only
matches = make_schedule(teams)
print("flawed matches:")
homeAwayRotation(matches)
main()
My expected results would be printing each round showing which team is versing which and each team having a history a bit like this:
a team in Group1 has a verse history of: (in any random order)
1v2, 2v1, 1v3, 3v1, 1v4, 4v1, 1v5, 1v6, 1v7, 1v8, bye
a team in Group2 has a verse history of: (in any random order)
5v1, 5v2, 5v3, 5v4, 5v6, 5v7, 5v8, 5v9 5v10, 5v11, 5v12
a team in Group3 has a verse history of: (in any random order)
9v10, 10v9, 9v11, 11v9, 9v12, 12v9, 9v5, 9v6, 9v7, 9v8, bye
Any pointers or improvements I could possibly do would be greatly appreciated as I have been stuck on the final hurdle for the last 2 weeks
If I have understood the problem correct, then all you need is some combining of teams with every member in different groups.
I put some code together that should solve your problem:
def vs(team, group):
matchups = map(lambda opponent: (team,opponent), group)
matchups = filter(lambda tup: tup[0] != tup[1], matchups)
return list(matchups)
def matches(teams):
group_size = len(teams) // 3
# Make the groups, basically just splitting the team list in three parts
groups = [teams[:group_size], teams[group_size:2*group_size], teams[2*group_size:]]
matchups = []
for index, team in enumerate(teams):
group_index = index // group_size
current_matchup = []
# Check if we're working with group 1 or 3
if group_index == 0 or group_index == 2:
# Flip the order of a tuple
def flip(x):
return (x[1], x[0])
own_group = vs(team, groups[group_index])
# Add matches against everyone in the group
current_matchup.extend(own_group)
# Add matches agains everyone in the group again, but now the current team is 'away'
current_matchup.extend(list(map(flip, own_group)))
# Add matches against everyone in group 2
current_matchup.extend(vs(team, groups[1]))
# Lastly, add the bye
current_matchup.append((team, "bye"))
else:
# Just all matches against all other teams, once.
current_matchup.extend(vs(team, teams))
matchups.append(current_matchup)
return matchups
# This can be anything. Numbers, 'Team 1' or even 'The wondrous flying squirrels of death'
teams = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
# Make matches
matchups = matches(teams)
# Just pretty print
for i in range(len(matchups)):
matches = '\n\t'.join(map(lambda m: f'{str(m[0]).rjust(10)} vs {str(m[1]).ljust(10)}', matchups[i]))
print(f"Team '{teams[i]}' matches:\n\t{matches}")
I'm making a dice game that rolls 5 dice in Python, if any 3(or more) of the dice match then I'm trying to make it the number on the die *100.
For example 3 ones would be 300. I have the random number generator worked out and my gameboard set up, i'm just not sure of the logic required to compare items on a list one buy one like this and then assign values. My first attempt at writing something this complex.
Just looking to get led in the right direction. Maybe an iter.tools function I overlooked would be useful for this?
Honestly been thinking about it for awhile and googling around, but can't come up with an answer.
import random
import itertools
useddice = 0
game = [[0, 0, 0, 0, 0],
[0, 0]]
#def choose_die(dielist):
#def score(dielist, ):
def dielist():
die1 = [random.randint(1,6)]
die2 = [random.randint(1,6)]
die3 = [random.randint(1,6)]
die4 = [random.randint(1,6)]
die5 = [random.randint(1,6)]
dielist=[die1, die2, die3, die4, die5]
print(dielist)
return dielist
def game_board(game_map, dielist, just_display=False):
print(" "+" ".join([str(i) for i in range(5)]), ' <Dice Number, NOT ROLL')
if not just_display:
game_map[column] = dielist
return game_map
dielist()
game_board(game, dielist, just_display=True)
#Everything from here is a work in progress
'''def win(score):
if score >= 10000:
print("Congratulations you won!")
return
play = True
players = [1, 2]
while play:
game = [0, 0, 0, 0, 0]
game_won = False
player_cycle = itertools.cycle([1, 2])
game_board(game, just_display=True)
while not game_won:
current_player = next(player_cycle)
played = False
while not played:
print(f"Player: {current_player}")
'''
Use the Counter class:
from collections import Counter
die_values = [random.randint(1,6) for _ in range(5)]
die_value_counts = Counter(die_values)
for val, cnt in die_value_counts.items():
if cnt >= 3:
# ... do other stuff ...
break
As a side remark, your code uses dielist both as a function name and a variable name. This can easily lead to problems and should be avoided.
The first thing you want to do is define a method that rolls the dices. What you had works perfectly fine, but here's a slightly optimized version:
import random
def roll_dices():
return [random.randint(0, 6) for n in range(5)]
dices = roll_dices()
print(dices) # outputs something like [5,2,4,5,0]
Now we'd like to know if any value appears 3 times or more. The shortest way to achieve this would be the following:
values = [x for x in set(dices) if dices.count(x) >= 3]
In short, this creates a new list of all the values that appear 3 times or more inside the dices list.
And here it is all together with the score and the players:
import random
import itertools
WINNING_SCORE = 10000;
class Player:
score = 0
def __init__(self, name):
self.name = name
def roll_dices():
return [random.randint(0, 6) for n in range(5)]
player_cycle = itertools.cycle([Player('John'), Player('Judy')])
while True:
current_player = next(player_cycle)
dices = roll_dices()
values = [x for x in set(dices) if dices.count(x) >= 3]
current_player.score += (sum(values) * 100)
print('{player} just played, and their new score is {score}'.format(player=current_player.name, score=current_player.score))
if(current_player.score >= WINNING_SCORE):
break
print('{player} won the game with {score} points!'.format(player=current_player.name, score=current_player.score))
I've also thrown in a Player class to make it easier to work with the scores and such.
I have used the module intervals (http://pyinterval.readthedocs.io/en/latest/index.html)
And created an interval from a set or start, end tuples:
intervals = interval.interval([1,8], [7,10], [15,20])
Which result in interval([1.0, 10.0], [15.0, 20.0]) as the [1,8] and [7,10] overlaps.
But this module interprets the values of the pairs as real numbers, so two continuous intervals in integers will not be joined together.
Example:
intervals = interval.interval([1,8], [9,10], [11,20])
results in: interval([1.0, 8.0], [9.0, 10.0], [11.0, 20.0])
My question is how can I join this intervals as integers and not as real numbers? And in the last example the result would be interval([1.0, 20.0])
The intervals module pyinterval is used for real numbers, not for integers. If you want to use objects, you can create an integer interval class or you can also code a program to join integer intervals using the interval module:
def join_int_intervlas(int1, int2):
if int(int1[-1][-1])+1 >= int(int2[-1][0]):
return interval.interval([int1[-1][0], int2[-1][-1]])
else:
return interval.interval()
I believe you can use pyintervals for integer intervals too by adding interval([-0.5, 0.5]). With your example you get
In[40]: interval([1,8], [9,10], [11,20]) + interval([-0.5, 0.5])
Out[40]: interval([0.5, 20.5])
This takes a list of tuples like l = [(25,24), (17,18), (5,9), (24,16), (10,13), (15,19), (22,25)]
# Idea by Ben Voigt in https://stackoverflow.com/questions/32869247/a-container-for-integer-intervals-such-as-rangeset-for-c
def sort_condense(ivs):
if len(ivs) == 0:
return []
if len(ivs) == 1:
if ivs[0][0] > ivs[0][1]:
return [(ivs[0][1], ivs[0][0])]
else:
return ivs
eps = []
for iv in ivs:
ivl = min(iv)
ivr = max(iv)
eps.append((ivl, False))
eps.append((ivr, True))
eps.sort()
ret = []
level = 0
i = 0
while i < len(eps)-1:
if not eps[i][1]:
level = level+1
if level == 1:
left = eps[i][0]
else:
if level == 1:
if not eps[i+1][1]
and eps[i+1][0] == eps[i][0]+1:
i = i+2
continue
right = eps[i][0]
ret.append((left, right))
level = level-1
i = i+1
ret.append((left, eps[len(eps)-1][0]))
return ret
In [1]: sort_condense(l)
Out[1]: [(5, 13), (15, 25)]
The idea is outlined in Ben Voigt's answer to A container for integer intervals, such as RangeSet, for C++
Python is not my main language, sorry.
I came up with the following program:
ls = [[1,8], [7,10], [15,20]]
ls2 = []
prevList = ls[0]
for lists in ls[1:]:
if lists[0] <= prevList[1]+1:
prevList = [prevList[0], lists[1]]
else:
ls2.append(prevList)
prevList = lists
ls2.append(prevList)
print ls2 # prints [[1, 10], [15, 20]]
It permutes through all lists and checks if the firsy element of each list is less than or equal to the previous element + 1. If so, it clubs the two.
I wanted to know if it will be possible to solve the Josepheus problem using list in python.
In simple terms Josephus problem is all about finding a position in a circular arrangement which would be safe if executions were handled out using a skip parameter which is known beforehand.
For eg : given a circular arrangement such as [1,2,3,4,5,6,7] and a skip parameter of 3, the people will be executed in the order as 3,6,2,7,5,1 and position 4 would be the safe.
I have been trying to solve this using list for some time now, but the index positions becomes tricky for me to handle.
a=[x for x in range(1,11)]
skip=2
step=2
while (len(a)!=1):
value=a[step-1]
a.remove(value)
n=len(a)
step=step+skip
large=max(a)
if step>=n:
diff=abs(large-value)
step=diff%skip
print a
Updated the question with code snippet, but i don't think my logic is correct.
Quite simply, you can use list.pop(i) to delete each victim (and get his ID) in a loop. Then, we just have to worry about wrapping the indices, which you can do just by taking the skipped index mod the number of remaining prisoners.
So then, the question solution becomes
def josephus(ls, skip):
skip -= 1 # pop automatically skips the dead guy
idx = skip
while len(ls) > 1:
print(ls.pop(idx)) # kill prisoner at idx
idx = (idx + skip) % len(ls)
print('survivor: ', ls[0])
Test output:
>>> josephus([1,2,3,4,5,6,7], 3)
3
6
2
7
5
1
survivor: 4
In [96]: def josephus(ls, skip):
...: from collections import deque
...: d = deque(ls)
...: while len(d)>1:
...: d.rotate(-skip)
...: print(d.pop())
...: print('survivor:' , d.pop())
...:
In [97]: josephus([1,2,3,4,5,6,7], 3)
3
6
2
7
5
1
survivor: 4
If you do not want to calculate the index, you can use the deque data structure.
My solution uses a math trick I found online here: https://www.youtube.com/watch?v=uCsD3ZGzMgE
It uses the binary way of writing the number of people in the circle and the position where the survivor sits. The result is the same and the code is shorter.
And the code is this:
numar_persoane = int(input("How many people are in the circle?\n")) #here we manually insert the number of people in the circle
x='{0:08b}'.format(int(numar_persoane)) #here we convert to binary
m=list(x) #here we transform it into a list
for i in range(0,len(m)): #here we remove the first '1' and append to the same list
m.remove('1')
m.append('1')
break
w=''.join(m) #here we make it a string again
print("The survivor sits in position",int(w, 2)) #int(w, 2) makes our string a decimal number
if you are looking for the final result only, here is a simple solution.
def JosephusProblem(people):
binary = bin(people) # Converting to binary
winner = binary[3:]+binary[2] # as the output looks like '0b101001'. removing 0b and adding the 1 to the end
print('The winner is',int(winner,2)) #converting the binary back to decimal
If you are looking for the math behind this code, go check out this video:
Josephus Problem(youTube)
it looks worse but easier to understand for beginners
def last(n):
a=[x for x in range(1,n+1)]
man_with_sword = 1
print(a)
while len(a)!=1:
if man_with_sword == a[len(a)-2]: #man_with_sword before last in circle
killed = a[len(a)-1]
a.remove(killed)
man_with_sword=a[0]
elif man_with_sword==a[len(a)-1]: #man_with_sword last in circle
killed = a[0]
a.remove(killed)
man_with_sword=a[0]
else:
i=0
while i < (len(a)//2):
i=a.index(man_with_sword)
killed = a[a.index(man_with_sword)+1]
a.remove(killed)
#pass the sword
man_with_sword=a[i+1] # pass the sword to next ( we killed next)
print (a, man_with_sword) #show who survived and sword owner
i+=1
print (a, man_with_sword,'next circle') #show who survived and sword owner
The total number of persons n and a number k, which indicates that k-1 persons are skipped and a kth person is killed in the circle.
def josephus(n, k):
if n == 1:
return 1
else:
return (josephus(n - 1, k) + k-1) % n + 1
n = 14
k = 2
print("The chosen place is ", josephus(n, k))
This is my solution to your question:
# simple queue implementation<ADT>
class Queue:
def __init__(self):
self.q = []
def enqueue(self,data):
self.q.insert(0,data)
def dequeue(self):
self.q.pop()
def sizeQ(self):
return len(self.q)
def printQ(self):
return self.q
lists = ["Josephus","Mark","Gladiator","Coward"]
to_die = 3
Q = Queue()
# inserting element into Q
for i in lists:
Q.enqueue(i)
# for size > 1
while Q.sizeP() > 1:
for j in range(1,3):
# every third element to be eliminated
Q.enqueue(Q.dequeue())
Q.dequeue()
print(Q.printQ())
def Last_Person(n):
person = [x for x in range(1,n+1)]
x = 0
c = 1
while len(person) > 1:
if x == len(person) - 1:
print("Round ", c, "- Here's who is left: ", person, "Person ", person[x], "killed person", person[0])
person.pop(0)
x = 0
c = c+1
elif x == len(person) - 2:
print("Round ", c, "- Here's who is left: ", person, "Person ", person[x], "killed person", person[x + 1])
person.pop(x+1)
x = 0
c = c + 1
else:
print("Round ", c, "- Here's who is left: ", person, "Person ", person[x], "killed person", person[x + 1])
person.pop(x + 1)
x = x + 1
c = c + 1
print("Person", person[x], "is the winner")
Last_Person(50)