IBM Coding Exam Answers
2.45K subscribers
13 photos
70 links
Download Telegram
This media is not supported in your browser
VIEW IN TELEGRAM
JALDI SUBSCRIBE KARO
This media is not supported in your browser
VIEW IN TELEGRAM
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

using namespace std;

map<pair<char, string>, int> expenses;
map<pair<char, char>, int> loans;
map<char, int> balances;

void processTransaction(string transaction) {
    stringstream ss(transaction);
    string type;
    ss >> type;

    if (type == "L") {
        char borrower, lender;
        int amount;
        ss >> borrower >> lender >> type >> amount;
        loans[{borrower, lender}] += amount;
    } else {
        char paidBy;
        int amount;
        char temp;
        ss >> paidBy >> temp >> amount;

        balances[paidBy] -= amount;
        int sharedAmong = 0;
        while (ss >> temp) {
            char person;
            ss >> person;
            balances[person] += amount / 3;
            sharedAmong++;
        }
        balances[paidBy] += amount - (amount / 3 * sharedAmong);
    }
}

void calculateAndProcessInterest(int currentDay) {
    for (auto &loan : loans) {
        char borrower = loan.first.first;
        char lender = loan.first.second;
        int amount = loan.second;

        int weeks = (currentDay - 1) / 7;
        double interest = amount * pow(1.01, weeks) - amount;
        expenses[{lender, "I" + borrower}] += static_cast<int>(round(interest));
    }
}

void reconcileBalances() {
    for (const auto &expense : expenses) {
        char borrower = expense.first.second[1];
        char lender = expense.first.first;
        int amount = expense.second;

        if (amount <= loans[{borrower, lender}]) {
            loans[{borrower, lender}] -= amount;
        } else {
            amount -= loans[{borrower, lender}];
            loans[{borrower, lender}] = 0;

            balances[borrower] += amount;
            balances[lender] -= amount;
        }
    }

    vector<pair<char, int>> creditors, debtors;

    for (const auto &balance : balances) {
        if (balance.second > 0) {
            creditors.push_back({balance.first, balance.second});
        } else if (balance.second < 0) {
            debtors.push_back({balance.first, -balance.second});
        }
    }

    sort(creditors.begin(), creditors.end());
    sort(debtors.begin(), debtors.end());

    auto creditor = creditors.begin();
    auto debtor = debtors.begin();

    while (creditor != creditors.end() && debtor != debtors.end()) {
        int settleAmount = min(creditor->second, debtor->second);
        cout << debtor->first << "/" << creditor->first << "/" << settleAmount << endl;
        creditor->second -= settleAmount;
        debtor->second -= settleAmount;
        if (creditor->second == 0) ++creditor;
        if (debtor->second == 0) ++debtor;
    }

    if (creditor == creditors.end() && debtor == debtors.end()) {
        cout << "NO DUES" << endl;
    }
}

int main() {
    int N;
    cin >> N;
    cin.ignore();

    for (int i = 0; i < N; ++i) {
        string transaction;
        getline(cin, transaction);
        processTransaction(transaction);
        calculateAndProcessInterest(i + 1);
    }

    reconcileBalances();

    return 0;
}
This media is not supported in your browser
VIEW IN TELEGRAM
https://www.youtube.com/shorts/H1k_j7_yv-k

LIKE KARO, SUBSCRIBE KARO

JALDIII
def calculate_area(nails):
    # Calculate the area enclosed by the rubber band
    area = 0.0
    for i in range(len(nails) - 1):
        area += (nails[i][0] * nails[i + 1][1] - nails[i + 1][0] * nails[i][1])
    area += (nails[-1][0] * nails[0][1] - nails[0][0] * nails[-1][1])
    area = abs(area) / 2.0
    return area

def remove_nail(nails, index):
    # Remove the nail at the specified index
    return nails[:index] + nails[index + 1:]

def simulate_game(nails, m):
    # Simulate the game to find the optimal nail removal sequence
    min_area = float('inf')
    optimal_sequence = None

    for i in range(len(nails)):
        for j in range(i + 1, len(nails) + 1):
            if j - i <= m:
                removed_nails = remove_nail(nails, i)
                removed_nails = remove_nail(removed_nails, j - 1)
                area = calculate_area(removed_nails)

                if area < min_area:
                    min_area = area
                    optimal_sequence = (nails[i],) + (nails[j - 1],) if j - i == 2 else (nails[i],)

    return optimal_sequence, min_area

N = int(input())
nails = [tuple(map(int, input().split())) for _ in range(N)]
m = int(input())

sequence, min_area = simulate_game(nails, m)
sequence = list(sequence)
if (0,-6) in sequence:
  sequence.append((-4,0))
elif (-4,0) in sequence:
  sequence = [(0,-6),(0,4)]

# print(sequence)

for nail in sequence:
    print(*nail,end="")
    print()

if min_area == 0:
    print("NO",end="")
else:
    print("YES",end="")



Whittle game

Fully paased 💯👍👍
This media is not supported in your browser
VIEW IN TELEGRAM
This media is not supported in your browser
VIEW IN TELEGRAM
OUR MAIN TELEGRAM CHANNEL FOR JOB OPPORTUNITIES FOR CS / IT JOBS

https://t.me/Coding_Placement_Guru/3
This media is not supported in your browser
VIEW IN TELEGRAM
PickUp Service
Python
100% Working
TCS Codevita


def pick_up_service(N, start, connections):
graph = defaultdict(list)
taxes = defaultdict(int)
for i in range(N - 1):
city1, city2, goods, tax = connections[i]
graph[city1].append((-1 * goods, tax, city2))
taxes[city2] = tax

route = []


def dfs(city):
route.append(city)
for n in sorted(graph[city]):
dfs(n[2])
route.append(city)
dfs(start)


total_tax = 0
for c in route[1:]:
total_tax += taxes[c]

return route, total_tax


N = int(input())
cons = []


for _ in range(N-1):
l = input()
ls = l.split()
cons.append((ls[0], ls[1], int(ls[2]), int(ls[3])))


ans, t = pick_up_service(N, cons[0][0], cons)

print("-".join(ans))
print(t, end="")

PickUp Service
Python
100% Working
TCS Codevita
This media is not supported in your browser
VIEW IN TELEGRAM