Snippets Collections
contract ExampleContract{
	function meaningOfLifeAndAllExistence() public pure returns (bytes memory) {
		return msg.data;
	}
}
contract ExampleContract {
	function meaningOfLifeAndAllExistence() public pure returns (uint256) {
		return 42;
	}
}
contract ExampleContract {

	function getTopLeaderboardScore() public pure returns (address, uint256) {
		return (0xd8da6bf26964af9d7eed9e03e53415d37aa96045, 100);
	}

	function highestScoreIsOver9000() public pure returns (bool) {
		(address leader, uint256 score) = getTopLeaderboardScore();

		if (score > 9000) {
			return true;
		}
	
		return false;	
	}
}
contract ExampleContract {

	function getTopLeaderboardScore() public pure returns (address, uint256) {
		return (0xd8da6bf26964af9d7eed9e03e53415d37aa96045, 100);
	}
}
contract ERC20 {
	string public name;
	string public symbol;

	mapping(address => uint256) public balanceOf;
	address public owner;
	uint8 public decimals;

	uint256 public totalSupply;

	// owner -> spender -> allowance
  	// this enables an owner to give allowance to multiple addresses
	mapping(address => mapping(address => uint256)) public allowance;

	constructor(string memory _name, string memory _symbol) {
		name = _name;
		symbol = _symbol;
		decimals = 18;

		owner = msg.sender;
	}

	function mint(address to, uint256 amount) public {
		require(msg.sender == owner, "only owner can create tokens");
		totalSupply += amount;
		balanceOf[owner] += amount;
	}

	function transfer(address to, uint256 amount) public returns (bool) {
		return helperTransfer(msg.sender, to, amount);
	}

	function approve(address spender, uint256 amount) public returns (bool) {
		allowance[msg.sender][spender] = amount;

		return true;
	}

	// just added
	function transferFrom(address from, address to, uint256 amount) public returns (bool) {
		if (msg.sender != from) {
			require(allowance[from][msg.sender] >= amount, "not enough allowance");

			allowance[from][msg.sender] -= amount;
		}

		return helperTransfer(from, to, amount);
	}


	// it's very important for this function to be internal!
	function helperTransfer(address from, address   to, uint256 amount) internal returns (bool) {
		require(balanceOf[from] >= amount, "not enough money");
		require(to != address(0), "cannot send to address(0)");
		balanceOf[from] -= amount;
		balanceOf[to] += amount;

		return true;
	}
}
contract ERC20 {
	string public name;
	string public symbol;

	mapping(address => uint256) public balanceOf;
	address public owner;
	uint8 public decimals;

	uint256 public totalSupply;

	// owner -> spender -> allowance
  // this enables an owner to give allowance to multiple addresses
	mapping(address => mapping(address => uint256)) public allowance;


	constructor(string memory _name, string memory _symbol) {
		name = _name;
		symbol = _symbol;
		decimals = 18;

		owner = msg.sender;
	}

	function mint(address to, uint256 amount) public {
		require(msg.sender == owner, "only owner can create tokens");
		totalSupply += amount;
		balanceOf[owner] += amount;
	}

	function transfer(address to, uint256 amount) public returns (bool) {
		require(balanceOf[msg.sender] >= amount, "you aint rich enough");
		require(to != address(0), "cannot send to address(0)");
		balanceOf[msg.sender] -= amount;
		balanceOf[to] += amount;

		return true;
	}

	function approve(address spender, uint256 amount) public returns (bool) {
		allowance[msg.sender][spender] = amount;

		return true;
	}

	// just added
	function transferFrom(address from, address to, uint256 amount) public returns (bool) {
		require(balanceOf[from] >= amount, "not enough money");
		require(to != address(0), "cannot send to address(0)");

		if (msg.sender != from) {
			require(allowance[from][msg.sender] >= amount, "not enough allowance");

			allowance[from][msg.sender] -= amount;
		}

		balanceOf[from] -= amount;
		balanceOf[to] += amount;

		return true;
	}
}
contract ERC20 {
	string public name;
	string public symbol;

	mapping(address => uint256) public balanceOf;
	address public owner;
	uint8 public decimals;
	uint256 public totalSupply;

	// owner -> spender -> allowance
  	// this enables an owner to give allowance to multiple addresses
	mapping(address => mapping(address => uint256)) public allowance;

	constructor(string memory _name, string memory _symbol) {
		name = _name;
		symbol = _symbol;
		decimals = 18;

		owner = msg.sender;
	}

	function mint(address to, uint256 amount) public {
		require(msg.sender == owner, "only owner can create tokens");
		totalSupply += amount;
		balanceOf[owner] += amount;
	}

	function transfer(address to, uint256 amount) public {
		require(balanceOf[msg.sender] >= amount, "you aint rich enough");
		require(to != address(0), "cannot send to address(0)");
		balanceOf[msg.sender] -= amount;
		balanceOf[to] += amount;
	}

	function approve(address spender, uint256 amount) public {
		allowance[msg.sender][spender] = amount;
	}

	// just added
	function transferFrom(address from, address to, uint256 amount) public {
		require(balanceOf[from] >= amount, "not enough money");

		if (msg.sender != from) {
			require(allowance[from][msg.sender] >= amount, "not enough allowance");

			allowance[from][msg.sender] -= amount;
		}

		balanceOf[from] -= amount;
		balanceOf[to] += amount;
	}
}
contract ERC20 {
	string public name;
	string public symbol;
	
	mapping(address => uint256) public balanceOf;
	address public owner;
	uint8 public decimals;

	uint256 public totalSupply;

	// owner -> spender -> allowance
  // this enables an owner to give allowance to multiple addresses
	mapping(address => mapping(address => uint256)) public allowance; // just added	address public owner;
	

	constructor(string memory _name, string memory _symbol) {
		name = _name;
		symbol = _symbol;
		decimals = 18;

		owner = msg.sender;
	}

	function mint(address to, uint256 amount) public {
		require(msg.sender == owner, "only owner can create tokens");
		totalSupply += amount;
		balanceOf[owner] += amount;
	}

	function transfer(address to, uint256 amount) public {
		require(balanceOf[msg.sender] >= amount, "you aint rich enough");
		require(to != address(0), "cannot send to address(0)");
		balanceOf[msg.sender] -= amount;
		balanceOf[to] += amount;
	}

	// just added
	function approve(address spender, uint256 amount) public {
		allowance[msg.sender][spender] = amount;
	}
}
contract ERC20 {
	string public name;
	string public symbol;

	mapping(address => uint256) public balanceOf;
	address public owner;
	uint8 public decimals;

	uint256 public totalSupply;

	constructor(string memory _name, string memory _symbol) {
		name = _name;
		symbol = _symbol;
		decimals = 18;

		owner = msg.sender;
	}

	function mint(address to, uint256 amount) public {
		require(msg.sender == owner, "only owner can create tokens");
		totalSupply += amount;
		balanceOf[owner] += amount;
	}

	function transfer(address to, uint256 amount) public {
		require(balanceOf[msg.sender] >= amount, "you aint rich enough");
		require(to != address(0), "cannot send to address(0)");
		balanceOf[msg.sender] -= amount;
		balanceOf[to] += amount;
	}
}
contract ERC20 {
	string public name;
	string public symbol;

	mapping(address => uint256) public balanceOf;
	address public owner;
	uint8 public decimals;

	uint256 public totalSupply;

	constructor(string memory _name, string memory _symbol, uint8 decimals) {
		name = _name;
		symbol = _symbol;
		decimals = 18;

		owner = msg.sender;
	}

	function mint(address to, uint256 amount) public {
		require(msg.sender == owner, "only owner can create tokens");
		totalSupply += amount;
		balanceOf[owner] += amount;
	}
}
contract ERC20 {
	string public name;
	string public symbol;

	mapping(address => uint256) public balanceOf;
	address public owner;

	uint256 public totalSupply;

	constructor(string memory _name, string memory _symbol) {
		name = _name;
		symbol = _symbol;

		owner = msg.sender;
	}

	function mint(address to, uint256 amount) public {
		require(msg.sender == owner, "only owner can create tokens");
		totalSupply += amount;
		balanceOf[owner] += amount;

	}

	function transfer(address to, uint256 amount) public {
		require(balanceOf[msg.sender] >= amount, "you aint rich enough");
		require(to != address(0), "cannot send to address(0)");
		balanceOf[msg.sender] -= amount;
		balanceOf[to] += amount;
	}
}
contract ERC20 {
	string public name;
	string public symbol;

	mapping(address => uint256) public balanceOf;
	address public owner;

	constructor(string memory _name, string memory _symbol) {
		name = _name;
		symbol = _symbol;

		owner = msg.sender;
	}

	function mint(address to, uint256 amount) public {
		require(msg.sender == owner, "only owner can create tokens");
		balanceOf[owner] += amount;
	}
}
contract ERC20 {
	string public name;
	string public symbol;

	mapping(address => uint256) public balanceOf;

	constructor(string memory _name, string memory _symbol) {
		name = _name;
		symbol = _symbol;
	}
}
contract ERC20 {
	string public name;
	string public symbol;

	constructor(string memory _name, string memory _symbol) {
		name = _name;
		symbol = _symbol;
	}
}
contract ExampleContract {
	function mustNotBeFive(uint256 x) public pure returns (uint256) {
		// valid, but bad practice
		require(x != 5);
		return x * 2;
	}
}
contract ExampleContract {
	function mustNotBeFive(uint256 x) public pure returns (uint256) {
		require(x != 5, "five is not valid");
		return x * 2;
	}
}
contract ExampleContract {
	string public name;

	// if you use calldata, it won't compile
	constructor(string memory _name) {
		name = _name;
	}
}
contract ExampleContract {

	address public banker;

	constructor(address _banker) {
		banker = _banker;
	}
}
contract ExampleContract {

	address public banker;

	constructor() {
		deployer = msg.sender;
	}
}
contract ERC20 {
	address public banker = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;

	mapping(address => uint256) public balances;

	function setSomeonesBalance(address owner, uint256 amount) public {
		if (msg.sender == banker) {
			balances[owner] = amount;
		}
		// do nothing
	}

	function transfer(address receiver, uint256 amount) public {
		balances[msg.sender] -= amount;
		balances[receiver] += amount;
	}
}
contract ExampleContract {
	function whoami() public view returns (address) {
		return address(this);
	}
}
contract ERC20 {
	address public banker = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;

	mapping(address => uint256) public balances;

	function setSomeonesBalance(address owner, uint256 amount) public {
		if (msg.sender == banker) {
			balances[owner] = amount;
		}
		// do nothing
	}

	function transfer(address receiver, uint256 amount) public {
		balances[msg.sender] -= amount;
		balances[receiver] += amount;
	}
}
contract ERC20Token {
	address public banker = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;

	mapping(address => uint256) public balances;

	function setSomeonesBalance(address owner, uint256 amount) public {
		if (msg.sender == banker) {
			balances[owner] = amount;
		}
		// do nothing
	}

	function transferTokensBetweenAddresses(address sender, address receiver, uint256 amount) public {
		if (msg.sender == banker) {
			balances[sender] -= amount;   // deduct/debit the sender's balance
			balances[receiver] += amount; // credit the reciever's balance
		}
		// do nothing
	}
}
contract ExampleContract {
		function whoami() public view returns (address) {
		address sender = msg.sender;
		return sender;
	}
}
class Solution {
    public boolean PredictTheWinner(int[] arr) {
        //dp playlist ques - 45 , use of gap strategy
        
        //finding total
        int n = arr.length , total = 0;

        for(int i = 0 ; i < n ; i++)
        total += arr[i];

        //making the dp
        int [][]dp = new int[n][n];
        
        //traversing & handling trivial cases and rest of the dp
        for(int gap = 0 ; gap <  n ; gap++){
            
            for(int i = 0 , j = gap ; j < n ; i++ , j++){
                
                //when only one coin
                if(gap == 0)
                dp[i][j] = arr[i];  //i==j
                
                //when only 2 coin, you take maximum of 2
                else if(gap == 1)
                dp[i][j] = Math.max(arr[i],arr[j]);
                
                //when more than 2 coins
                else{
                    /* if you chose ith coin , the opponent will choose the
                    next coin such that you get minimum of next choice
                    */
                    int cost1 = arr[i] + Math.min(dp[i+2][j],dp[i+1][j-1]);
                    
                    /* if you chose ith coin , the opponent will choose the
                    next coin such that you get minimum of next choice
                    */
                    int cost2 = arr[j] + Math.min(dp[i+1][j-1] , dp[i][j-2]);
                    
                    //but you will make choices such that you get max of cost1
                    //and cost2
                    dp[i][j] = Math.max(cost1 , cost2);
                }
            }
        }
        //now your final maximum score is in dp[0][n-1] , opponent score will be
        int opponent = total - dp[0][n-1];
        
        //if opponent score is greater than yours return false else true
        return opponent > dp[0][n-1] ? false : true;
    }
}








contract ERC20Token {
	mapping(address => uint256) public balances;

	function setSomeonesBalance(address owner, uint256 amount) public {
		balances[owner] = amount;
	}

	function transferTokensBetweenAddresses(address sender, address receiver, uint256 amount) public {
		balances[sender] -= amount;   // deduct/debit the sender's balance
		balances[receiver] += amount; // credit the reciever's balance
	}
}
contract TrackDebt {

	mapping(address => mapping(address => uint256)) public amountOwed;

	function setAmountOwed(address borrower, address lender, uint256 amount) public {
		amountOwed[lender][borrower] = amount;
	}
}
contract ExampleContract {
	mapping(uint256 => mapping(uint256 => uint256)) public nestedMap;

	function setNestedMap(uint256 key1, uint256 key2, uint256 finalValue) public {
		nestedMap[key1][key2] = finalValue;
	}

	function getNestedMap(uint256 key1, uint256 key2, uint256 finalValue) public {
		nestedMap[key1][key2] = finalValue;
	}
}
contract BrokenContract {
	mapping(uint256 => uint256) public someMap;

	function wontWork() public view returns (mapping(uint256 => uint256)) {
		return someMap;
	}
}
contract BrokenContract {
	mapping(uint256 => uint256) public someMap;

	function wontWork() public view {
		for (key in someMap) {
			// do something
		}
	}
}
contract BrokenContract {

	function wontWork() public view {
		mapping(uint256 => uint256) someMap;
	}
}
contract ERC20Token {

	mapping(address => uint256) public balances;

	function setSomeonesBalance(address owner, uint256 amount) public {
		balances[owner] = amount;
	}

	function transferTokensBetweenAddresses(address sender, address receiver, uint256 amount) public {
		balances[sender] -= amount;   // deduct/debit the sender's balance
		balances[receiver] += amount; // credit the reciever's balance
	}
}
contract ExampleContract {
  // returns false by default
	mapping(uint256 => bool) public mapToBool;

	// returns 0 by default
	mapping(uint256 => uint256) public mapToUint; 

	// returns 0x0000000000000000000000000000000000000000 by default
	mapping(uint256 => address) public mapToAddress;

}
contract ExampleContract {

	mapping(uint256 => uint256) public myMapping;

	function setMapping(uint256 key, uint256 value) public {
		myMapping[key] = value;
	}

	function getValue(uint256 key) public view returns (uint256) {
		return myMapping[key];
	}
}
contract ExampleContract {

	string public name;

	function setName(string calldata newName) public {
		name = newName;
	}
}
contract ExampleContract {
	
	uint256[] public myArray;

	function popAndSwap(uint256 index) public {
		uint256 valueAtTheEnd = myArray[myArray.length - 1];
		myArray.pop(); // reduces the length;
		myArray[index] = valueAtTheEnd;
	}
}
contract ExampleContract {
	
	uint256[] public myArray;

	function removeAt(uint256 index) public {
		delete myArray[index];
		// sets the value at index to be zero

		// the following code is equivalent
		// myArray[index] = 0;

		// myArray.length does not change in either circumstance
	}
}
function myArray(uint256 index) public view returns (uint256) {
	return myArray[index];
}
contract ExampleContract {

	uint256[] public myArray;

	function setMyArray(uint256[] calldata newArray) public {
		myArray = newArray;
	}

	function addToArray(uint256 newItem) public {
		myArray.push(newItem);
	}

	function removeFromArray() public {
		myArray.pop();
	}

	function getLength() public view returns (uint256) {
		return myArray.length;
	}

	function getEntireArray() public view returns (uint256[] memory) {
		return myArray;
	}
}
contract ExampleContract {

	uint256 public x;

	function setX(uint256 newValue) public {
		x = newValue;
	}
	
	function getX() public view returns (uint256) {
		return x;
	}
}
contract ExampleContract {

	uint256 x;

	function setX(uint256 newValue) public {
		x = newValue;
	}
	
	function getX() public view returns (uint256) {
		return x;
	}
}
contract ExampleContract {

	uint256 internal x;

	function setX(uint256 newValue) public {
		x = newValue;
	}
	
	// error: this function cannot be pure
	function getX() public pure returns (uint256) {
		return x;
	}
}
contract ExampleContract {

	uint256 internal x;

	function setX(uint256 newValue) public {
		x = newValue;
	}
	
	function getX() public view returns (uint256) {
		return x;
	}
}
contract ExampleContract {

	// ACCEPTED: [[1,2],[3,4],[5,6]] -> returns 6
	function getLast(uint256[2][3] calldata nestedArray) public pure returns (uint256) {
		return nestedArray[2][1];

		// nestedArray[2] -> [5,6] then get the 1st index item -> 6
	}
}
contract ExampleContract {

	// ACCEPTED: [[1,2],[3,4],[5,6]]
	// REJECTED: [[1,2,3],[4,5,6]]
	function fixedSize(uint256[2][3] calldata nestedArray) public pure returns (uint256) {
		return 0; // just for the sake of compilation
	}
}
contract ExampleContract {

	
	// [[1,2],[3,4],[5,6]] becomes [1,2]
	function getRow(uint256[][] calldata nestedArray) public pure returns(uint256[] memory) {
		return nestedArray[0];
	}
}
contract ExampleContract {

	function containsAThree(uint256[][] calldata nestedArray) public pure returns (bool) {

		for (uint256 i = 0; i < nestedArray.length; i++) {
			for (uint256 j = 0; j < nestedArray[0].length; j++) {
				if (nestedArray[i][j] == 3) {
					return true;
				}
			}
		}
		return false;
	}
}
pragma solidity ^0.8.12;
contract StringContract {

	function useArrays(string calldata input) public pure returns(uint256) {
		return input.length; // does not compile
	}
}
pragma solidity ^0.8.12;
contract BadContract {

	function useArrays(string calldata input) public pure returns(string memory) {
		return input[0]; // error
	}
}
star

Thu Mar 30 2023 11:27:35 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:27:15 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:26:46 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:26:27 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:25:33 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:25:08 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:24:51 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:24:09 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:23:54 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:23:33 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:22:20 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:21:55 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:21:35 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:20:55 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:20:15 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:19:51 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:19:26 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:19:03 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:18:32 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:18:09 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:17:17 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:16:51 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:16:27 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 11:16:04 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:46:05 GMT+0000 (UTC) https://leetcode.com/problems/predict-the-winner/

@Ayush_dabas07

star

Thu Mar 30 2023 10:11:52 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:11:00 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:09:32 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:09:03 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:08:23 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:05:12 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:04:50 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:04:28 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:04:00 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:03:18 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:03:02 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:02:44 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:01:39 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:01:16 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:00:50 GMT+0000 (UTC) https://www.mammaknowsparties.com.au/venue-holding-page

@uneakrao

star

Thu Mar 30 2023 10:00:43 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 10:00:13 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 09:59:53 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 09:59:36 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 09:58:53 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 09:58:17 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 09:57:32 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 09:57:13 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 09:56:11 GMT+0000 (UTC)

@RareSkills

star

Thu Mar 30 2023 09:55:52 GMT+0000 (UTC)

@RareSkills

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension