Untitled

mail@pastecode.io avatar
unknown
python
7 months ago
3.6 kB
4
Indexable
Never

def sentTimes(numberofPorts, transmissionTime, packetIds):
	my_dict = {} # (port Id, timestamp to allow packet in)
	for i in range(numberofPorts):
		my_dict[i] = 0


	results = []
	for i in range(len(packetIds)):
		initial = packetIds[i] % numberofPorts
		while True:
			if my_dict[initial] < i + 1:
				results.append(initial)
				my_dict[initial] = i + transmissionTime
				break
			else:
				if initial == len(packetIds) - 1:
					initial = 0
				else:
					initial += 1

	return results


result = sentTimes(3, 2, [4, 7, 10, 6])
print("result = ", result)





def getTotalEfficiency(skill):
	total = sum(skill)
	count = len(skill)
	if count % 2 == 1:
		return -1
	
	count = int(count / 2)
	if total % count != 0:
		return -1
	
	team_sum = int(total / count)


	answer = 0
	valid_count = 0
	my_dict = {}
	for s in skill:
		if s in my_dict and my_dict[s] > 0:
			tmp = s * (team_sum - s)
			answer += tmp
			my_dict[s] -= 1
			valid_count += 1
		else:
			target = team_sum - s
			if target in my_dict:
				my_dict[target] += 1
			else:
				my_dict[target] = 1

	if count == valid_count:
		return answer
	else:
		return -1



result = getTotalEfficiency([1, 2, 3, 2])
print(result)


result = getTotalEfficiency([2, 1, 1, 4, 3, 5])
print(result)








def handleFiles(baseFilename):
	f = open(baseFilename, "r")
	all_lines = f.readlines()

	name1 = "c_" + baseFilename
	f1 = open(name1, "w")
	f1_a = open(name1, "a")

	name2 = "cpp_" + baseFilename
	f2 = open(name2, "w")
	f2_a = open(name2, "a")

	name3 = "cs_" + baseFilename
	f3 = open(name3, "w")
	f3_a = open(name3, "a")

	count1 = 0
	count2 = 0
	count3 = 0
	for line in all_lines:
		line = line.strip()
		if line.endswith(".c"):
			if count1 > 0:
				f1_a.write("\n")
			f1_a.write(line)
			count1 += 1
		elif line.endswith(".cpp"):
			if count2 > 0:
				f2_a.write("\n")
			f2_a.write(line)
			count2 += 1
		elif line.endswith(".cs"):
			if count3 > 0:
				f3_a.write("\n")
			f3_a.write(line)
			count3 += 1

	f1.close()
	f2.close()
	f3.close()
	f1_a.close()
	f2_a.close()
	f3_a.close()
	print("Done file write")



handleFiles("file_00.txt")






def maxProfit(price, profit):
    length = len(price)
    best_sum = -1
    for j in range(1, length-1):
    	best_i = -1
    	for i in range(j):
    		if price[i] < price[j]:
    			if best_i == -1 or profit[i] > profit[best_i]:
    				best_i = i

    	best_k = -1
    	for k in range(j+1, length):
    		if price[k] > price[j]:
    			if best_k == -1 or profit[k] > profit[best_k]:
    				best_k = k

    	if best_i != -1 and best_k != -1:
    		tmp = profit[best_i] + profit[j] + profit[best_k]
    		best_sum = max(best_sum, tmp)

    return best_sum




# Example usage:
price = [1, 2, 3, 4, 5]
profit = [1, 2, 3, 4, 5]
result = maxProfit(price, profit)
print("Maximum profit:", result)


price = [1, 5, 3, 4, 6]
profit = [2, 3, 4, 5, 6]
result = maxProfit(price, profit)
print("Maximum profit:", result)






def inverseNumbers(nums, updates):
	for update in updates:
		low = update[0]-1
		up = update[1]
		for i in range(low, up):
			nums[i] = nums[i] * -1

	return nums


result = inverseNumbers([1, 2, 3, 4], [[1, 2], [2, 4]])
print(result)






def getMinimumCost(arr):
	largest_diff = 0
	idx = 0
	for i in range(len(arr)-1):
		diff = abs(arr[i] - arr[i+1])
		if diff > largest_diff:
			largest_diff = diff
			idx = i

	tmp = arr[idx+1]
	arr[idx+1] = int((arr[idx] + arr[idx+1]) / 2)
	arr.append(tmp)

	cost = 0
	for i in range(len(arr)-1):
		cost += ((arr[i] - arr[i+1]) * (arr[i] - arr[i+1]))

	return cost



result = getMinimumCost([1, 3, 5, 2, 10])
print(result)