Transposición de caracteres en una cadena -- algorithm campo con strings campo con rust camp codereview Relacionados El problema

Transposing characters in a string


6
vote

problema

Español

Quiero crear una función que toma una cadena y devuelve una matriz de versiones en la que siempre se transpone un par de caracteres (intercambiado).

Se me ocurrió esto:

  import os def cls():     os.system('cls' if os.name=='nt' else 'clear') cls() 5  

La salida es:

  import os def cls():     os.system('cls' if os.name=='nt' else 'clear') cls() 6  

Ese resultado es lo que quiero, lo hace:

  • primer intercambio import os def cls(): os.system('cls' if os.name=='nt' else 'clear') cls() 7 y import os def cls(): os.system('cls' if os.name=='nt' else 'clear') cls() 38
  • luego intercambiar import os def cls(): os.system('cls' if os.name=='nt' else 'clear') cls() 9 y 'use strict'; document.getElementById('clear-cards').style.display = 'inline-block'; window.onbeforeunload = function() { return "Are you sure you want to leave the game?"; }; var count = 0; var lastPlayer = 'computer'; var humanCard; var humanCardNumber; var computerCard; var computerCardNumber; var humanCards = []; var computerCards = []; var human = { population: 100, foodGained: 0, soldiersGained: 0, soldiers: function soldiers() { return Math.round((this.population / 10) + this.soldiersGained); }, food: function food() { return Math.round(this.population * 2 + this.foodGained); } }; var computer = { population: 100, foodGained: 0, soldiersGained: 0, soldiers: function soldiers() { return Math.round((this.population / 10) + this.soldiersGained); }, food: function food() { return Math.round(this.population * 2 + this.foodGained); } }; // GET CARDS function getCards(cards) { while (cards.length <= 10) { var random = Math.ceil(Math.random() * 9); switch (random) { case 1: cards.push('recruit'); cards.push(Math.ceil(Math.random() * 6)); break; case 2: cards.push('ambush'); cards.push(1 * Math.ceil(Math.random() * 3)); break; case 3: cards.push('rampage'); cards.push(10 * Math.ceil(Math.random() * 2)); break; case 4: cards.push('harvest'); cards.push(10 * Math.ceil(Math.random() * 2)); break; case 5: cards.push('loot'); cards.push(10 * Math.ceil(Math.random() * 2)); break; case 6: cards.push('diplomacy'); cards.push(5 * Math.ceil(Math.random() * 3)); break; case 7: cards.push('coup'); cards.push(10 * Math.ceil(Math.random() * 2)); break; case 8: cards.push('traitors'); cards.push(Math.ceil(Math.random() * 3)); break; case 9: cards.push('growth'); cards.push(10 * Math.ceil(Math.random() * 2)); break; } } }; function displayCards(cards) { var j = 1; for (var i = 0; i <= 12; i += 2) { var type = humanCards[i]; var number = humanCards[i + 1]; var index = i; var card = 'img/cards/' + type + number + '.svg'; if (j <= 6) { document.getElementById([j]).innerHTML = ' <div onclick="playCard('' + type + '',' + number + ',' + index + ')"><img src="' + card + '" alt="Playing card"></div> '; j++; } } document.getElementById('cards').style.display = 'inline-block'; }; // runs when user clicks on card function playCard(type, number, index) { var amount; switch (type) { case 'recruit': human.soldiersGained += number; break; case 'ambush': computer.soldiersGained -= number; computer.population -= number; break; case 'rampage': computer.foodGained -= computer.food() / 100 * number; break; case 'harvest': human.foodGained += human.food() / 100 * number; break; case 'loot': amount = computer.food() / 100 * number; computer.foodGained -= amount; human.foodGained += amount; break; case 'diplomacy': amount = computer.population / 100 * number; computer.population -= amount; human.population += amount; break; case 'coup': computer.population -= computer.population / 100 * number; break; case 'traitors': computer.soldiersGained -= number; computer.population -= number; human.soldiersGained += number; human.population += number; break; case 'growth': human.population += human.population / 100 * number; break; } displayScore(); humanCards.splice(index, 2); humanCard = type; humanCardNumber = number; newTurn(); }; // NORMAL TURNS function newTurn() { document.getElementById('human-played').style.display = 'none'; document.getElementById('computer-played').style.display = 'none'; document.getElementById('war-or-peace').style.display = 'none'; document.getElementById('next').style.display = 'none'; if (computer.food() <= 0 || human.food() <= 0) { human.food() <= 0 ? endGame('humanFood') : endGame('computerFood'); } else if (computer.population <= 0 || human.population <= 0) { human.population <= 0 ? endGame('humanPopulation') : endGame('computerPopulation'); } else if (computer.soldiers() <= 0 || human.soldiers() <= 0) { human.soldiers() <= 0 ? endGame('humanSoldiers') : endGame('computerSoldiers'); } else if ((count === 9 || count === 19) && lastPlayer === 'computer') { count++; displayWarOrPeace(); } else if (lastPlayer === 'human') { document.getElementById('human-played').innerHTML = ' <img src="img/cards/' + humanCard + humanCardNumber + '.svg" alt="Playing card"><br><p>Your card</p> '; lastPlayer = 'computer'; computerTurn(); } else if (lastPlayer === 'computer') { lastPlayer = 'human'; count++; getCards(humanCards); getCards(computerCards); displayCards(humanCards); document.getElementById('clear-cards').style.display = 'inline-block'; document.getElementById('round').innerHTML = '<strong>Round ' + count + '</strong>'; } }; function clearCards() { if (lastPlayer === 'human') { humanCards = []; humanCard = 'get-new-cards'; humanCardNumber = 1; newTurn(); } else { computerCards = []; computerCard = 'get-new-cards'; computerCardNumber = 1; } }; function displayScore() { document.getElementById('round').innerHTML = '<strong>Round ' + count + '</strong>'; document.getElementById('human-score').innerHTML = ' <p> <strong>You</strong><br>' + Math.round(human.population) + ' Population<br>' + human.soldiers() + ' Soldiers<br>' + human.food() + ' Food </p> '; document.getElementById('computer-score').innerHTML = ' <p> <strong>Robots</strong><br>' + Math.round(computer.population) + ' Population<br>' + computer.soldiers() + ' Soldiers<br>' + computer.food() + ' Food </p> '; }; // WAR OR PEACE function displayWarOrPeace() { displayScore(); document.getElementById('cards').style.display = 'none'; document.getElementById('war-or-peace').style.display = 'inline-block'; document.getElementById('button').innerHTML = '<a onclick="warOrPeace()" class="buttons">Skip</a>'; if (human.soldiers() > computer.soldiers()) { document.getElementById('war').innerHTML = ' <div onclick="warOrPeace('war')"><img src="img/cards/war.svg" alt="War card">'; } else { document.getElementById('war').innerHTML = 'You do not have enough soliders to play war'; } if (human.food() > 200) { document.getElementById('peace').innerHTML = ' <div class="war-or-peace" onclick="warOrPeace('peace')"><img src="img/cards/peace.svg" alt="Peace card"></div> ' } else { document.getElementById('peace').innerHTML = 'You do not have enough food to play peace'; } }; function warOrPeace(humanOption) { var warBenefit = computer.soldiers() * 2; var peaceBenefit = computer.population / 2; var computerChoice; var decreaseHumanPopulation; var increaseComputerPopulation; var number1; var number2; // Computer chooses war or peace and saves its effect without playing it if (computer.soldiers() > human.soldiers() && (warBenefit > peaceBenefit || computer.food() < 200)) { decreaseHumanPopulation = computer.soldiers() * 3; computerChoice = 'war'; } else if (computer.food() >= 200) { increaseComputerPopulation = Math.round(computer.population) / 2; computerChoice = 'peace'; } else { computerChoice = 'none'; } // human card played if (humanOption === 'war') { computer.population -= human.soldiers() * 3; number1 = 'Opponent population is reduced by ' + human.soldiers() * 3; } else if (humanOption === 'peace') { // var increaseHumanPopulation = Math.round(human.population) / 2; number1 = 'Population is increased by ' + Math.round(human.population / 2); human.population += Math.round(human.population) / 2; } else { number1 = 'No change to population'; humanOption = 'none'; } // computer card played if (computerChoice === 'war') { human.population -= decreaseHumanPopulation; number2 = 'Opponent population is reduced by ' + Math.round(decreaseHumanPopulation); } else if (computerChoice === 'peace') { computer.population += increaseComputerPopulation; number2 = 'Population is increased by ' + Math.round(increaseComputerPopulation); } else { number2 = 'No change to population'; } displayWarResult(humanOption, computerChoice, number1, number2); }; function displayWarResult(humanChoice, computerChoice, number1, number2) { document.getElementById('war').innerHTML = 'You chose ' + humanChoice + '. ' + number1; document.getElementById('peace').innerHTML = 'The robots chose ' + computerChoice + '. ' + number2; if (count < 15) { document.getElementById('button').innerHTML = '<a onclick="newTurn()" class="buttons">NEXT ROUND</a>'; displayScore(); } else { document.getElementById('cards').style.display = 'none'; document.getElementById('button').innerHTML = '<a onclick="end()" class="buttons">NEXT ROUND</a>'; } }; function end() { human.population >= computer.population ? endGame('human21') : endGame('computer21'); }; // COMPUTER AI function computerTurn() { document.getElementById('cards').style.display = 'none'; document.getElementById('human-played').style.display = 'inline-block'; document.getElementById('computer-loading').style.display = 'inline-block'; logic(); function wait() { document.getElementById('computer-loading').style.display = 'none'; document.getElementById('computer-played').style.display = 'inline-block'; document.getElementById('computer-played').innerHTML = ' <img src="img/cards/' + computerCard + computerCardNumber + '.svg" alt="Playing card"><br><p>Robots card</p> '; document.getElementById('next').style.display = 'inline-block'; displayScore(); }setTimeout(wait, 1400); }; function logic() { var myarray = []; // loop though each card in computerCards array. Assign a value to each card and find the card with the highest value for (var i = 0; i <= 11; i += 2) { var number = computerCards[i + 1]; var value; // console.log('Card: ' + computerCards[i]); // console.log('Number: ' + computerCards[i+1]); switch (computerCards[i]) { // food case 'rampage': value = number / 20 * 1.1; computer.food() < 250 ? value = value * 1.3 : value = value; computer.food() < human.food() ? value = value * 1.5 : value = value; break; case 'harvest': value = number / 20 * 1.3; computer.food() < 250 ? value = value * 1.3 : value = value; computer.food() < human.food() ? value = value * 1.4 : value = value; break; case 'loot': value = number / 20 * 1.5; computer.food() < 250 ? value = value * 1.3 : value = value; computer.food() < human.food() ? value = value * 1.4 : value = value; break; // soldiers case 'recruit': value = number / 6 * 2.4; computer.soldiers() < 5 ? value = value * 2 : value = value; computer.soldiers() < human.soldiers() ? value = value * 1.4 : value = value; break; case 'ambush': value = number / 6 * 2.4; if (computer.soldiers() < 5 || human.soldiers() > 15 || human.soldiers() < 5) { value = value * 2; } computer.soldiers() < human.soldiers() ? value = value * 1.4 : value = value; break; case 'traitors': value = number / 3 * 2.8; if (computer.soldiers() < 5 || human.soldiers() > 15 || human.soldiers() < 5) { value = value * 2; } computer.soldiers() < human.soldiers() ? value = value * 1.4 : value = value; break; // population case 'diplomacy': value = number / 15 * 4; count > 10 ? value = value * 1.1 : value = value; human.population > computer.population ? value = value * 2 : value = value; break; case 'coup': value = number / 20 * 3; count > 10 ? value = value * 1.1 : value = value; human.population > computer.population ? value = value * 2 : value = value; break; case 'growth': value = number / 30 * 4; count > 10 ? value = value * 1.1 : value = value; human.population > computer.population ? value = value * 2 : value = value; break; } // console.log('Value: ' + value); // console.log('________'); myarray.push(value); } var maxN = Math.max.apply(null, myarray); if (maxN < 1.5 && count < 18) { clearCards(); } else { var index = myarray.reduce(function (iMax, x, i, arr) { return x > arr[iMax] ? i : iMax; }, 0); var type = computerCards[index * 2]; var number = computerCards[index * 2 + 1]; var index = [index * 2]; computerPlayCard(type, number, index); computerCard = type; computerCardNumber = number; } }; function computerPlayCard(type, number, index) { var amount; switch (type) { case 'recruit': computer.soldiersGained += number; break; case 'ambush': human.soldiersGained -= number; human.population -= number; break; case 'rampage': human.foodGained -= human.food() / 100 * number; break; case 'harvest': computer.foodGained += computer.food() / 100 * number; break; case 'loot': amount = human.food() / 100 * number; human.foodGained -= amount; computer.foodGained += amount; break; case 'diplomacy': amount = human.population / 100 * number; human.population -= amount; computer.population += amount; break; case 'coup': human.population = human.population - (human.population / 100 * number); break; case 'traitors': human.soldiersGained -= number; human.population -= number; computer.soldiersGained += number; computer.population += number; break; case 'growth': computer.population += computer.population / 100 * number; break; } computerCards.splice(index, 2); computerCard = type; }; function endGame(reason) { var who; var why; switch (reason) { case 'human21': who = 'You'; why = 'You have more population!'; break; case 'computer21': who = 'The robots'; why = 'The robots have more population.'; break; case 'humanWar': who = 'You'; why = 'You won the war!'; break; case 'humanPeace': who = 'You'; why = 'You have more food.'; break; case 'computerWar': who = 'The robots'; why = 'The robots won the war!'; break; case 'computerPeace': who = 'The robots'; why = 'The robots won the war.'; break; case 'draw': who = 'Nobody gets to'; why = 'It's a draw!'; break; case 'humanFood': who = 'The robots'; why = 'You have no food!'; break; case 'computerFood': who = 'You'; why = 'The robots have no food!'; break; case 'humanSoldiers': who = 'The robots'; why = 'You have no soldiers to defend your people!'; break; case 'computerSoldiers': who = 'You'; why = 'The robots have no soldiers to defend themselves!'; break; case 'humanPopulation': who = 'The robots'; why = 'You have no people left!'; break; case 'computerPopulation': who = 'You'; why = 'There are no robots left!'; break; } displayScore(); document.getElementById('war-or-peace').style.display = 'none'; document.getElementById('winner').style.display = 'inline-block'; document.getElementById('winner').innerHTML = ' <h1>' + why + ' <br>' + who + ' win!</h1> <a onclick="newGame();" class="buttons mid-button">Play again?</a>'; var won; if (who === 'You') { won = 1; } else { won = 0; } var reason = 7; var url = 'save/' + won + '/' + reason; callAjax(url); }; newTurn(); displayScore(); function newGame() { document.getElementById('clear-cards').style.display = 'inline-block'; document.documentElement.scrollTop = 0; count = 0; lastPlayer = 'computer'; humanCards = []; computerCards = []; human.population = 100; human.foodGained = 0; human.soldiersGained = 0; computer.population = 100; computer.foodGained = 0; computer.soldiersGained = 0; human.soldiers(); computer.soldiers(); human.food(); computer.food(); document.getElementById('winner').style.display = 'none'; document.body.scrollTop = document.documentElement.scrollTop = 0; newTurn(); displayScore(); }; function callAjax(url){ var params = "_token=" + window.Laravel['csrfToken']; var xmlhttp; xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4 && xmlhttp.status == 200){ // console.log(xmlhttp.responseText); // alert('sent'); } // console.log(xmlhttp.responseText); } xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // xmlhttp.send(); xmlhttp.send(params); }; 0
  • luego intercambiar 'use strict'; document.getElementById('clear-cards').style.display = 'inline-block'; window.onbeforeunload = function() { return "Are you sure you want to leave the game?"; }; var count = 0; var lastPlayer = 'computer'; var humanCard; var humanCardNumber; var computerCard; var computerCardNumber; var humanCards = []; var computerCards = []; var human = { population: 100, foodGained: 0, soldiersGained: 0, soldiers: function soldiers() { return Math.round((this.population / 10) + this.soldiersGained); }, food: function food() { return Math.round(this.population * 2 + this.foodGained); } }; var computer = { population: 100, foodGained: 0, soldiersGained: 0, soldiers: function soldiers() { return Math.round((this.population / 10) + this.soldiersGained); }, food: function food() { return Math.round(this.population * 2 + this.foodGained); } }; // GET CARDS function getCards(cards) { while (cards.length <= 10) { var random = Math.ceil(Math.random() * 9); switch (random) { case 1: cards.push('recruit'); cards.push(Math.ceil(Math.random() * 6)); break; case 2: cards.push('ambush'); cards.push(1 * Math.ceil(Math.random() * 3)); break; case 3: cards.push('rampage'); cards.push(10 * Math.ceil(Math.random() * 2)); break; case 4: cards.push('harvest'); cards.push(10 * Math.ceil(Math.random() * 2)); break; case 5: cards.push('loot'); cards.push(10 * Math.ceil(Math.random() * 2)); break; case 6: cards.push('diplomacy'); cards.push(5 * Math.ceil(Math.random() * 3)); break; case 7: cards.push('coup'); cards.push(10 * Math.ceil(Math.random() * 2)); break; case 8: cards.push('traitors'); cards.push(Math.ceil(Math.random() * 3)); break; case 9: cards.push('growth'); cards.push(10 * Math.ceil(Math.random() * 2)); break; } } }; function displayCards(cards) { var j = 1; for (var i = 0; i <= 12; i += 2) { var type = humanCards[i]; var number = humanCards[i + 1]; var index = i; var card = 'img/cards/' + type + number + '.svg'; if (j <= 6) { document.getElementById([j]).innerHTML = ' <div onclick="playCard('' + type + '',' + number + ',' + index + ')"><img src="' + card + '" alt="Playing card"></div> '; j++; } } document.getElementById('cards').style.display = 'inline-block'; }; // runs when user clicks on card function playCard(type, number, index) { var amount; switch (type) { case 'recruit': human.soldiersGained += number; break; case 'ambush': computer.soldiersGained -= number; computer.population -= number; break; case 'rampage': computer.foodGained -= computer.food() / 100 * number; break; case 'harvest': human.foodGained += human.food() / 100 * number; break; case 'loot': amount = computer.food() / 100 * number; computer.foodGained -= amount; human.foodGained += amount; break; case 'diplomacy': amount = computer.population / 100 * number; computer.population -= amount; human.population += amount; break; case 'coup': computer.population -= computer.population / 100 * number; break; case 'traitors': computer.soldiersGained -= number; computer.population -= number; human.soldiersGained += number; human.population += number; break; case 'growth': human.population += human.population / 100 * number; break; } displayScore(); humanCards.splice(index, 2); humanCard = type; humanCardNumber = number; newTurn(); }; // NORMAL TURNS function newTurn() { document.getElementById('human-played').style.display = 'none'; document.getElementById('computer-played').style.display = 'none'; document.getElementById('war-or-peace').style.display = 'none'; document.getElementById('next').style.display = 'none'; if (computer.food() <= 0 || human.food() <= 0) { human.food() <= 0 ? endGame('humanFood') : endGame('computerFood'); } else if (computer.population <= 0 || human.population <= 0) { human.population <= 0 ? endGame('humanPopulation') : endGame('computerPopulation'); } else if (computer.soldiers() <= 0 || human.soldiers() <= 0) { human.soldiers() <= 0 ? endGame('humanSoldiers') : endGame('computerSoldiers'); } else if ((count === 9 || count === 19) && lastPlayer === 'computer') { count++; displayWarOrPeace(); } else if (lastPlayer === 'human') { document.getElementById('human-played').innerHTML = ' <img src="img/cards/' + humanCard + humanCardNumber + '.svg" alt="Playing card"><br><p>Your card</p> '; lastPlayer = 'computer'; computerTurn(); } else if (lastPlayer === 'computer') { lastPlayer = 'human'; count++; getCards(humanCards); getCards(computerCards); displayCards(humanCards); document.getElementById('clear-cards').style.display = 'inline-block'; document.getElementById('round').innerHTML = '<strong>Round ' + count + '</strong>'; } }; function clearCards() { if (lastPlayer === 'human') { humanCards = []; humanCard = 'get-new-cards'; humanCardNumber = 1; newTurn(); } else { computerCards = []; computerCard = 'get-new-cards'; computerCardNumber = 1; } }; function displayScore() { document.getElementById('round').innerHTML = '<strong>Round ' + count + '</strong>'; document.getElementById('human-score').innerHTML = ' <p> <strong>You</strong><br>' + Math.round(human.population) + ' Population<br>' + human.soldiers() + ' Soldiers<br>' + human.food() + ' Food </p> '; document.getElementById('computer-score').innerHTML = ' <p> <strong>Robots</strong><br>' + Math.round(computer.population) + ' Population<br>' + computer.soldiers() + ' Soldiers<br>' + computer.food() + ' Food </p> '; }; // WAR OR PEACE function displayWarOrPeace() { displayScore(); document.getElementById('cards').style.display = 'none'; document.getElementById('war-or-peace').style.display = 'inline-block'; document.getElementById('button').innerHTML = '<a onclick="warOrPeace()" class="buttons">Skip</a>'; if (human.soldiers() > computer.soldiers()) { document.getElementById('war').innerHTML = ' <div onclick="warOrPeace('war')"><img src="img/cards/war.svg" alt="War card">'; } else { document.getElementById('war').innerHTML = 'You do not have enough soliders to play war'; } if (human.food() > 200) { document.getElementById('peace').innerHTML = ' <div class="war-or-peace" onclick="warOrPeace('peace')"><img src="img/cards/peace.svg" alt="Peace card"></div> ' } else { document.getElementById('peace').innerHTML = 'You do not have enough food to play peace'; } }; function warOrPeace(humanOption) { var warBenefit = computer.soldiers() * 2; var peaceBenefit = computer.population / 2; var computerChoice; var decreaseHumanPopulation; var increaseComputerPopulation; var number1; var number2; // Computer chooses war or peace and saves its effect without playing it if (computer.soldiers() > human.soldiers() && (warBenefit > peaceBenefit || computer.food() < 200)) { decreaseHumanPopulation = computer.soldiers() * 3; computerChoice = 'war'; } else if (computer.food() >= 200) { increaseComputerPopulation = Math.round(computer.population) / 2; computerChoice = 'peace'; } else { computerChoice = 'none'; } // human card played if (humanOption === 'war') { computer.population -= human.soldiers() * 3; number1 = 'Opponent population is reduced by ' + human.soldiers() * 3; } else if (humanOption === 'peace') { // var increaseHumanPopulation = Math.round(human.population) / 2; number1 = 'Population is increased by ' + Math.round(human.population / 2); human.population += Math.round(human.population) / 2; } else { number1 = 'No change to population'; humanOption = 'none'; } // computer card played if (computerChoice === 'war') { human.population -= decreaseHumanPopulation; number2 = 'Opponent population is reduced by ' + Math.round(decreaseHumanPopulation); } else if (computerChoice === 'peace') { computer.population += increaseComputerPopulation; number2 = 'Population is increased by ' + Math.round(increaseComputerPopulation); } else { number2 = 'No change to population'; } displayWarResult(humanOption, computerChoice, number1, number2); }; function displayWarResult(humanChoice, computerChoice, number1, number2) { document.getElementById('war').innerHTML = 'You chose ' + humanChoice + '. ' + number1; document.getElementById('peace').innerHTML = 'The robots chose ' + computerChoice + '. ' + number2; if (count < 15) { document.getElementById('button').innerHTML = '<a onclick="newTurn()" class="buttons">NEXT ROUND</a>'; displayScore(); } else { document.getElementById('cards').style.display = 'none'; document.getElementById('button').innerHTML = '<a onclick="end()" class="buttons">NEXT ROUND</a>'; } }; function end() { human.population >= computer.population ? endGame('human21') : endGame('computer21'); }; // COMPUTER AI function computerTurn() { document.getElementById('cards').style.display = 'none'; document.getElementById('human-played').style.display = 'inline-block'; document.getElementById('computer-loading').style.display = 'inline-block'; logic(); function wait() { document.getElementById('computer-loading').style.display = 'none'; document.getElementById('computer-played').style.display = 'inline-block'; document.getElementById('computer-played').innerHTML = ' <img src="img/cards/' + computerCard + computerCardNumber + '.svg" alt="Playing card"><br><p>Robots card</p> '; document.getElementById('next').style.display = 'inline-block'; displayScore(); }setTimeout(wait, 1400); }; function logic() { var myarray = []; // loop though each card in computerCards array. Assign a value to each card and find the card with the highest value for (var i = 0; i <= 11; i += 2) { var number = computerCards[i + 1]; var value; // console.log('Card: ' + computerCards[i]); // console.log('Number: ' + computerCards[i+1]); switch (computerCards[i]) { // food case 'rampage': value = number / 20 * 1.1; computer.food() < 250 ? value = value * 1.3 : value = value; computer.food() < human.food() ? value = value * 1.5 : value = value; break; case 'harvest': value = number / 20 * 1.3; computer.food() < 250 ? value = value * 1.3 : value = value; computer.food() < human.food() ? value = value * 1.4 : value = value; break; case 'loot': value = number / 20 * 1.5; computer.food() < 250 ? value = value * 1.3 : value = value; computer.food() < human.food() ? value = value * 1.4 : value = value; break; // soldiers case 'recruit': value = number / 6 * 2.4; computer.soldiers() < 5 ? value = value * 2 : value = value; computer.soldiers() < human.soldiers() ? value = value * 1.4 : value = value; break; case 'ambush': value = number / 6 * 2.4; if (computer.soldiers() < 5 || human.soldiers() > 15 || human.soldiers() < 5) { value = value * 2; } computer.soldiers() < human.soldiers() ? value = value * 1.4 : value = value; break; case 'traitors': value = number / 3 * 2.8; if (computer.soldiers() < 5 || human.soldiers() > 15 || human.soldiers() < 5) { value = value * 2; } computer.soldiers() < human.soldiers() ? value = value * 1.4 : value = value; break; // population case 'diplomacy': value = number / 15 * 4; count > 10 ? value = value * 1.1 : value = value; human.population > computer.population ? value = value * 2 : value = value; break; case 'coup': value = number / 20 * 3; count > 10 ? value = value * 1.1 : value = value; human.population > computer.population ? value = value * 2 : value = value; break; case 'growth': value = number / 30 * 4; count > 10 ? value = value * 1.1 : value = value; human.population > computer.population ? value = value * 2 : value = value; break; } // console.log('Value: ' + value); // console.log('________'); myarray.push(value); } var maxN = Math.max.apply(null, myarray); if (maxN < 1.5 && count < 18) { clearCards(); } else { var index = myarray.reduce(function (iMax, x, i, arr) { return x > arr[iMax] ? i : iMax; }, 0); var type = computerCards[index * 2]; var number = computerCards[index * 2 + 1]; var index = [index * 2]; computerPlayCard(type, number, index); computerCard = type; computerCardNumber = number; } }; function computerPlayCard(type, number, index) { var amount; switch (type) { case 'recruit': computer.soldiersGained += number; break; case 'ambush': human.soldiersGained -= number; human.population -= number; break; case 'rampage': human.foodGained -= human.food() / 100 * number; break; case 'harvest': computer.foodGained += computer.food() / 100 * number; break; case 'loot': amount = human.food() / 100 * number; human.foodGained -= amount; computer.foodGained += amount; break; case 'diplomacy': amount = human.population / 100 * number; human.population -= amount; computer.population += amount; break; case 'coup': human.population = human.population - (human.population / 100 * number); break; case 'traitors': human.soldiersGained -= number; human.population -= number; computer.soldiersGained += number; computer.population += number; break; case 'growth': computer.population += computer.population / 100 * number; break; } computerCards.splice(index, 2); computerCard = type; }; function endGame(reason) { var who; var why; switch (reason) { case 'human21': who = 'You'; why = 'You have more population!'; break; case 'computer21': who = 'The robots'; why = 'The robots have more population.'; break; case 'humanWar': who = 'You'; why = 'You won the war!'; break; case 'humanPeace': who = 'You'; why = 'You have more food.'; break; case 'computerWar': who = 'The robots'; why = 'The robots won the war!'; break; case 'computerPeace': who = 'The robots'; why = 'The robots won the war.'; break; case 'draw': who = 'Nobody gets to'; why = 'It's a draw!'; break; case 'humanFood': who = 'The robots'; why = 'You have no food!'; break; case 'computerFood': who = 'You'; why = 'The robots have no food!'; break; case 'humanSoldiers': who = 'The robots'; why = 'You have no soldiers to defend your people!'; break; case 'computerSoldiers': who = 'You'; why = 'The robots have no soldiers to defend themselves!'; break; case 'humanPopulation': who = 'The robots'; why = 'You have no people left!'; break; case 'computerPopulation': who = 'You'; why = 'There are no robots left!'; break; } displayScore(); document.getElementById('war-or-peace').style.display = 'none'; document.getElementById('winner').style.display = 'inline-block'; document.getElementById('winner').innerHTML = ' <h1>' + why + ' <br>' + who + ' win!</h1> <a onclick="newGame();" class="buttons mid-button">Play again?</a>'; var won; if (who === 'You') { won = 1; } else { won = 0; } var reason = 7; var url = 'save/' + won + '/' + reason; callAjax(url); }; newTurn(); displayScore(); function newGame() { document.getElementById('clear-cards').style.display = 'inline-block'; document.documentElement.scrollTop = 0; count = 0; lastPlayer = 'computer'; humanCards = []; computerCards = []; human.population = 100; human.foodGained = 0; human.soldiersGained = 0; computer.population = 100; computer.foodGained = 0; computer.soldiersGained = 0; human.soldiers(); computer.soldiers(); human.food(); computer.food(); document.getElementById('winner').style.display = 'none'; document.body.scrollTop = document.documentElement.scrollTop = 0; newTurn(); displayScore(); }; function callAjax(url){ var params = "_token=" + window.Laravel['csrfToken']; var xmlhttp; xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4 && xmlhttp.status == 200){ // console.log(xmlhttp.responseText); // alert('sent'); } // console.log(xmlhttp.responseText); } xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // xmlhttp.send(); xmlhttp.send(params); }; 1 y 'use strict'; document.getElementById('clear-cards').style.display = 'inline-block'; window.onbeforeunload = function() { return "Are you sure you want to leave the game?"; }; var count = 0; var lastPlayer = 'computer'; var humanCard; var humanCardNumber; var computerCard; var computerCardNumber; var humanCards = []; var computerCards = []; var human = { population: 100, foodGained: 0, soldiersGained: 0, soldiers: function soldiers() { return Math.round((this.population / 10) + this.soldiersGained); }, food: function food() { return Math.round(this.population * 2 + this.foodGained); } }; var computer = { population: 100, foodGained: 0, soldiersGained: 0, soldiers: function soldiers() { return Math.round((this.population / 10) + this.soldiersGained); }, food: function food() { return Math.round(this.population * 2 + this.foodGained); } }; // GET CARDS function getCards(cards) { while (cards.length <= 10) { var random = Math.ceil(Math.random() * 9); switch (random) { case 1: cards.push('recruit'); cards.push(Math.ceil(Math.random() * 6)); break; case 2: cards.push('ambush'); cards.push(1 * Math.ceil(Math.random() * 3)); break; case 3: cards.push('rampage'); cards.push(10 * Math.ceil(Math.random() * 2)); break; case 4: cards.push('harvest'); cards.push(10 * Math.ceil(Math.random() * 2)); break; case 5: cards.push('loot'); cards.push(10 * Math.ceil(Math.random() * 2)); break; case 6: cards.push('diplomacy'); cards.push(5 * Math.ceil(Math.random() * 3)); break; case 7: cards.push('coup'); cards.push(10 * Math.ceil(Math.random() * 2)); break; case 8: cards.push('traitors'); cards.push(Math.ceil(Math.random() * 3)); break; case 9: cards.push('growth'); cards.push(10 * Math.ceil(Math.random() * 2)); break; } } }; function displayCards(cards) { var j = 1; for (var i = 0; i <= 12; i += 2) { var type = humanCards[i]; var number = humanCards[i + 1]; var index = i; var card = 'img/cards/' + type + number + '.svg'; if (j <= 6) { document.getElementById([j]).innerHTML = ' <div onclick="playCard('' + type + '',' + number + ',' + index + ')"><img src="' + card + '" alt="Playing card"></div> '; j++; } } document.getElementById('cards').style.display = 'inline-block'; }; // runs when user clicks on card function playCard(type, number, index) { var amount; switch (type) { case 'recruit': human.soldiersGained += number; break; case 'ambush': computer.soldiersGained -= number; computer.population -= number; break; case 'rampage': computer.foodGained -= computer.food() / 100 * number; break; case 'harvest': human.foodGained += human.food() / 100 * number; break; case 'loot': amount = computer.food() / 100 * number; computer.foodGained -= amount; human.foodGained += amount; break; case 'diplomacy': amount = computer.population / 100 * number; computer.population -= amount; human.population += amount; break; case 'coup': computer.population -= computer.population / 100 * number; break; case 'traitors': computer.soldiersGained -= number; computer.population -= number; human.soldiersGained += number; human.population += number; break; case 'growth': human.population += human.population / 100 * number; break; } displayScore(); humanCards.splice(index, 2); humanCard = type; humanCardNumber = number; newTurn(); }; // NORMAL TURNS function newTurn() { document.getElementById('human-played').style.display = 'none'; document.getElementById('computer-played').style.display = 'none'; document.getElementById('war-or-peace').style.display = 'none'; document.getElementById('next').style.display = 'none'; if (computer.food() <= 0 || human.food() <= 0) { human.food() <= 0 ? endGame('humanFood') : endGame('computerFood'); } else if (computer.population <= 0 || human.population <= 0) { human.population <= 0 ? endGame('humanPopulation') : endGame('computerPopulation'); } else if (computer.soldiers() <= 0 || human.soldiers() <= 0) { human.soldiers() <= 0 ? endGame('humanSoldiers') : endGame('computerSoldiers'); } else if ((count === 9 || count === 19) && lastPlayer === 'computer') { count++; displayWarOrPeace(); } else if (lastPlayer === 'human') { document.getElementById('human-played').innerHTML = ' <img src="img/cards/' + humanCard + humanCardNumber + '.svg" alt="Playing card"><br><p>Your card</p> '; lastPlayer = 'computer'; computerTurn(); } else if (lastPlayer === 'computer') { lastPlayer = 'human'; count++; getCards(humanCards); getCards(computerCards); displayCards(humanCards); document.getElementById('clear-cards').style.display = 'inline-block'; document.getElementById('round').innerHTML = '<strong>Round ' + count + '</strong>'; } }; function clearCards() { if (lastPlayer === 'human') { humanCards = []; humanCard = 'get-new-cards'; humanCardNumber = 1; newTurn(); } else { computerCards = []; computerCard = 'get-new-cards'; computerCardNumber = 1; } }; function displayScore() { document.getElementById('round').innerHTML = '<strong>Round ' + count + '</strong>'; document.getElementById('human-score').innerHTML = ' <p> <strong>You</strong><br>' + Math.round(human.population) + ' Population<br>' + human.soldiers() + ' Soldiers<br>' + human.food() + ' Food </p> '; document.getElementById('computer-score').innerHTML = ' <p> <strong>Robots</strong><br>' + Math.round(computer.population) + ' Population<br>' + computer.soldiers() + ' Soldiers<br>' + computer.food() + ' Food </p> '; }; // WAR OR PEACE function displayWarOrPeace() { displayScore(); document.getElementById('cards').style.display = 'none'; document.getElementById('war-or-peace').style.display = 'inline-block'; document.getElementById('button').innerHTML = '<a onclick="warOrPeace()" class="buttons">Skip</a>'; if (human.soldiers() > computer.soldiers()) { document.getElementById('war').innerHTML = ' <div onclick="warOrPeace('war')"><img src="img/cards/war.svg" alt="War card">'; } else { document.getElementById('war').innerHTML = 'You do not have enough soliders to play war'; } if (human.food() > 200) { document.getElementById('peace').innerHTML = ' <div class="war-or-peace" onclick="warOrPeace('peace')"><img src="img/cards/peace.svg" alt="Peace card"></div> ' } else { document.getElementById('peace').innerHTML = 'You do not have enough food to play peace'; } }; function warOrPeace(humanOption) { var warBenefit = computer.soldiers() * 2; var peaceBenefit = computer.population / 2; var computerChoice; var decreaseHumanPopulation; var increaseComputerPopulation; var number1; var number2; // Computer chooses war or peace and saves its effect without playing it if (computer.soldiers() > human.soldiers() && (warBenefit > peaceBenefit || computer.food() < 200)) { decreaseHumanPopulation = computer.soldiers() * 3; computerChoice = 'war'; } else if (computer.food() >= 200) { increaseComputerPopulation = Math.round(computer.population) / 2; computerChoice = 'peace'; } else { computerChoice = 'none'; } // human card played if (humanOption === 'war') { computer.population -= human.soldiers() * 3; number1 = 'Opponent population is reduced by ' + human.soldiers() * 3; } else if (humanOption === 'peace') { // var increaseHumanPopulation = Math.round(human.population) / 2; number1 = 'Population is increased by ' + Math.round(human.population / 2); human.population += Math.round(human.population) / 2; } else { number1 = 'No change to population'; humanOption = 'none'; } // computer card played if (computerChoice === 'war') { human.population -= decreaseHumanPopulation; number2 = 'Opponent population is reduced by ' + Math.round(decreaseHumanPopulation); } else if (computerChoice === 'peace') { computer.population += increaseComputerPopulation; number2 = 'Population is increased by ' + Math.round(increaseComputerPopulation); } else { number2 = 'No change to population'; } displayWarResult(humanOption, computerChoice, number1, number2); }; function displayWarResult(humanChoice, computerChoice, number1, number2) { document.getElementById('war').innerHTML = 'You chose ' + humanChoice + '. ' + number1; document.getElementById('peace').innerHTML = 'The robots chose ' + computerChoice + '. ' + number2; if (count < 15) { document.getElementById('button').innerHTML = '<a onclick="newTurn()" class="buttons">NEXT ROUND</a>'; displayScore(); } else { document.getElementById('cards').style.display = 'none'; document.getElementById('button').innerHTML = '<a onclick="end()" class="buttons">NEXT ROUND</a>'; } }; function end() { human.population >= computer.population ? endGame('human21') : endGame('computer21'); }; // COMPUTER AI function computerTurn() { document.getElementById('cards').style.display = 'none'; document.getElementById('human-played').style.display = 'inline-block'; document.getElementById('computer-loading').style.display = 'inline-block'; logic(); function wait() { document.getElementById('computer-loading').style.display = 'none'; document.getElementById('computer-played').style.display = 'inline-block'; document.getElementById('computer-played').innerHTML = ' <img src="img/cards/' + computerCard + computerCardNumber + '.svg" alt="Playing card"><br><p>Robots card</p> '; document.getElementById('next').style.display = 'inline-block'; displayScore(); }setTimeout(wait, 1400); }; function logic() { var myarray = []; // loop though each card in computerCards array. Assign a value to each card and find the card with the highest value for (var i = 0; i <= 11; i += 2) { var number = computerCards[i + 1]; var value; // console.log('Card: ' + computerCards[i]); // console.log('Number: ' + computerCards[i+1]); switch (computerCards[i]) { // food case 'rampage': value = number / 20 * 1.1; computer.food() < 250 ? value = value * 1.3 : value = value; computer.food() < human.food() ? value = value * 1.5 : value = value; break; case 'harvest': value = number / 20 * 1.3; computer.food() < 250 ? value = value * 1.3 : value = value; computer.food() < human.food() ? value = value * 1.4 : value = value; break; case 'loot': value = number / 20 * 1.5; computer.food() < 250 ? value = value * 1.3 : value = value; computer.food() < human.food() ? value = value * 1.4 : value = value; break; // soldiers case 'recruit': value = number / 6 * 2.4; computer.soldiers() < 5 ? value = value * 2 : value = value; computer.soldiers() < human.soldiers() ? value = value * 1.4 : value = value; break; case 'ambush': value = number / 6 * 2.4; if (computer.soldiers() < 5 || human.soldiers() > 15 || human.soldiers() < 5) { value = value * 2; } computer.soldiers() < human.soldiers() ? value = value * 1.4 : value = value; break; case 'traitors': value = number / 3 * 2.8; if (computer.soldiers() < 5 || human.soldiers() > 15 || human.soldiers() < 5) { value = value * 2; } computer.soldiers() < human.soldiers() ? value = value * 1.4 : value = value; break; // population case 'diplomacy': value = number / 15 * 4; count > 10 ? value = value * 1.1 : value = value; human.population > computer.population ? value = value * 2 : value = value; break; case 'coup': value = number / 20 * 3; count > 10 ? value = value * 1.1 : value = value; human.population > computer.population ? value = value * 2 : value = value; break; case 'growth': value = number / 30 * 4; count > 10 ? value = value * 1.1 : value = value; human.population > computer.population ? value = value * 2 : value = value; break; } // console.log('Value: ' + value); // console.log('________'); myarray.push(value); } var maxN = Math.max.apply(null, myarray); if (maxN < 1.5 && count < 18) { clearCards(); } else { var index = myarray.reduce(function (iMax, x, i, arr) { return x > arr[iMax] ? i : iMax; }, 0); var type = computerCards[index * 2]; var number = computerCards[index * 2 + 1]; var index = [index * 2]; computerPlayCard(type, number, index); computerCard = type; computerCardNumber = number; } }; function computerPlayCard(type, number, index) { var amount; switch (type) { case 'recruit': computer.soldiersGained += number; break; case 'ambush': human.soldiersGained -= number; human.population -= number; break; case 'rampage': human.foodGained -= human.food() / 100 * number; break; case 'harvest': computer.foodGained += computer.food() / 100 * number; break; case 'loot': amount = human.food() / 100 * number; human.foodGained -= amount; computer.foodGained += amount; break; case 'diplomacy': amount = human.population / 100 * number; human.population -= amount; computer.population += amount; break; case 'coup': human.population = human.population - (human.population / 100 * number); break; case 'traitors': human.soldiersGained -= number; human.population -= number; computer.soldiersGained += number; computer.population += number; break; case 'growth': computer.population += computer.population / 100 * number; break; } computerCards.splice(index, 2); computerCard = type; }; function endGame(reason) { var who; var why; switch (reason) { case 'human21': who = 'You'; why = 'You have more population!'; break; case 'computer21': who = 'The robots'; why = 'The robots have more population.'; break; case 'humanWar': who = 'You'; why = 'You won the war!'; break; case 'humanPeace': who = 'You'; why = 'You have more food.'; break; case 'computerWar': who = 'The robots'; why = 'The robots won the war!'; break; case 'computerPeace': who = 'The robots'; why = 'The robots won the war.'; break; case 'draw': who = 'Nobody gets to'; why = 'It's a draw!'; break; case 'humanFood': who = 'The robots'; why = 'You have no food!'; break; case 'computerFood': who = 'You'; why = 'The robots have no food!'; break; case 'humanSoldiers': who = 'The robots'; why = 'You have no soldiers to defend your people!'; break; case 'computerSoldiers': who = 'You'; why = 'The robots have no soldiers to defend themselves!'; break; case 'humanPopulation': who = 'The robots'; why = 'You have no people left!'; break; case 'computerPopulation': who = 'You'; why = 'There are no robots left!'; break; } displayScore(); document.getElementById('war-or-peace').style.display = 'none'; document.getElementById('winner').style.display = 'inline-block'; document.getElementById('winner').innerHTML = ' <h1>' + why + ' <br>' + who + ' win!</h1> <a onclick="newGame();" class="buttons mid-button">Play again?</a>'; var won; if (who === 'You') { won = 1; } else { won = 0; } var reason = 7; var url = 'save/' + won + '/' + reason; callAjax(url); }; newTurn(); displayScore(); function newGame() { document.getElementById('clear-cards').style.display = 'inline-block'; document.documentElement.scrollTop = 0; count = 0; lastPlayer = 'computer'; humanCards = []; computerCards = []; human.population = 100; human.foodGained = 0; human.soldiersGained = 0; computer.population = 100; computer.foodGained = 0; computer.soldiersGained = 0; human.soldiers(); computer.soldiers(); human.food(); computer.food(); document.getElementById('winner').style.display = 'none'; document.body.scrollTop = document.documentElement.scrollTop = 0; newTurn(); displayScore(); }; function callAjax(url){ var params = "_token=" + window.Laravel['csrfToken']; var xmlhttp; xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4 && xmlhttp.status == 200){ // console.log(xmlhttp.responseText); // alert('sent'); } // console.log(xmlhttp.responseText); } xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // xmlhttp.send(); xmlhttp.send(params); }; 2

No encuentro el código súper legible debido a la secuencia de cuerdas que está pasando. En C sería un mero "intercambio de puntero", pero mi versión de óxido se ve un poco más involucrada.

¿Hay una mejor manera de hacer lo que quiero?

Es un supuesto justo que esto solo funcionará correctamente para las cadenas ASCII.

Original en ingles

I want to create a function that takes a string and returns an array of versions of it in which always one pair of characters is transposed (swapped).

I came up with this:

fn transposes(s: &str) -> Vec<String> {     let mut vec = vec![];      let b = s.as_bytes();      for i in 1..b.len() {         let mut transpose = String::with_capacity(s.len());         transpose.push_str(&s[..i - 1]);         transpose.push(s.as_bytes()[i] as char);         transpose.push(s.as_bytes()[i - 1] as char);         transpose.push_str(&s[i + 1..]);          vec.push(transpose);     }      vec }  fn main() {     let s = "maxi".to_owned();     println!("{:?}", transposes(&s));    } 

Output is:

["amxi", "mxai", "maix"] 

That result is what I want, it does:

  • First swap m and a
  • Then swap a and x
  • Then swap i and x

I don't find the code super readable because of the string slicing going on. In C it would be a mere "pointer swap", but my Rust version looks a bit more involved.

Is there a better way to do what I want?

It is a fair assumption that this will only work correctly for ASCII strings.

        

Lista de respuestas

4
 
vote
vote
La mejor respuesta
 
  1. Si es solo ASCII, trátelo como una matriz de bytes hasta el final. calloc7 tiene un método 99887766555443328 y puede reconstruir un 99887776655443329 de los bytes.
  2. Uso sb_as_string()0 sb_as_string()11 En lugar de asignar un sb_as_string()2 y manualmente sb_as_string()33 ING.
  3. No es necesario asignar un sb_as_string()4 para pasar a sb_as_string()5 .
  sb_as_string()6  

aún mejor, si sus datos son ASCII, codifican que en el sistema tipo :

  sb_as_string()7  

Tenga en cuenta que la función 998877665554433388 ya no puede fallar debido a las cadenas no válidas.

 
  1. If it's just ASCII, treat it as a byte array all the way until the end. Vec has a swap method and you can reconstruct a String from the bytes.
  2. Use map and collect instead of allocating a Vec and manually pushing.
  3. There's no need to allocate a String to pass to &str.
fn transposes(s: &str) -> Vec<String> {     let bytes = s.as_bytes();      (1..bytes.len()).map(|i| {         let mut transpose = bytes.to_owned();         transpose.swap(i - 1, i);         String::from_utf8(transpose).expect("Invalid UTF-8")     }).collect() }  fn main() {     println!("{:?}", transposes("maxi")); } 

Even better, if your data is ASCII, encode that in the type system:

extern crate ascii;  use ascii::{AsciiStr, AsciiString};  fn transposes(s: &AsciiStr) -> Vec<AsciiString> {     (1..s.len()).map(|i| {         let mut transpose = s.to_owned();         transpose.as_mut_slice().swap(i - 1, i);         transpose     }).collect() }  fn main() {     let s = AsciiStr::from_ascii("maxi").expect("Not ASCII");     println!("{:?}", transposes(s)); } 

Note that the transposes function can no longer fail due to invalid strings.

 
 
       
       

Relacionados problema

6  Utilidad X-up para EVE Online  ( X up utility for eve online ) 
Estoy aprendiendo a Rust. También interpreto a Eve Online: un videojuego sobre las naves espaciales de Internet. Decidí que sería divertido practicar el óxi...

7  Implementar una secuencia de Fibonacci genérica en óxido sin usar copia rasgo  ( Implement a generic fibonacci sequence in rust without using copy trait ) 
Estoy tratando de aprender a óxido y soy un principiante. ¿Cómo se hace frente a la implementación de una versión genérica de la secuencia FIBONACCI sin usar ...

6  Clasificación de palabras por frecuencia  ( Sorting words by frequency ) 
Estoy haciendo una tarea simple en óxido después de leer el Libro de óxido : Lea un archivo de texto dividirlo en Whitespace desinfectar palabras elim...

3  Lista doblemente vinculada en óxido usando los punteros crudos  ( Doubly linked list in rust using raw pointers ) 
Estoy practicando la oxidación escribiendo una lista doblemente vinculada usando los punteros crudos, 9988776655544330 Para asignar datos en el montón, 998...

1  La mejor implementación de la mejor búsqueda de primera búsqueda en óxido  ( Greedy best first search implementation in rust ) 
He implementado un codicioso algoritmo de primera búsqueda en Rust, ya que no pude encontrar una ya implementada en las cajas existentes. Tengo un pequeño pro...

3  Base64 String ↔ Array flotante  ( Base64 string %e2%86%94 float array ) 
Necesito convertir move constructor8 matrices con una longitud de fix a base64 Representación y atrás. Mi código actual se ve así. Funciona, pero se sient...

8  Reemplace la cadena en el archivo  ( Replace string in file ) 
Este es mi primer módulo que escribí en el óxido. Soy nuevo en el idioma y no pude encontrar una manera de reemplazar fácilmente algunas palabras en un archiv...

5  Canción de 99 cervezas en óxido  ( 99 beers song in rust ) 
Estoy aprendiendo a Rust. Me parece que a veces se confuso en comparación con otros lenguajes de programación, especialmente cuando se trata de cadenas y re...

3  Aplicación singularidad e IPC unilateral en UNIX  ( Application uniqueness and unilateral ipc on unix ) 
este programa Detecta la singularidad de la aplicación, si la aplicación es una instancia única / primaria, lanza un servidor, de lo contrario, un cliente...

7  HMAC con SHA256 / 512 en óxido  ( Hmac with sha256 512 in rust ) 
He intentado lo mejor que puedo, para implementar HMAC como se especifica en la RFC 2104 < / a>. Este es también mi primer proyecto de óxido, por lo que las ...




© 2022 respuesta.top Reservados todos los derechos. Centro de preguntas y respuestas reservados todos los derechos