The 15th-century Venetian navigator Alvise Cadamosto describes a curious convention by which the Mauritanian Azanaghi traded salt with the merchants of Mali:
All Mauritanians who have salt pile it in rows, each marking their own. Having made these piles, the whole caravan retires half a day’s journey. Then there comes the Malians who do not wish to be seen or to speak. They arrive in large boats, from which it appears that they come from islands, and disembark. Seeing the salt, they place a large quantity of gold opposite each pile, and then turn back, leaving salt and gold. When they have gone, the Mauretanians return: if they are satisfied with the quantity of gold, they leave the salt and retire with the gold. Then the Malians return, and remove those piles which are without gold. By the other piles of salt they place more gold, if it pleases them, or else they leave the salt. In this way, by long and ancient custom, they carry on their trade without seeing or speaking to each other.
In this way different cultures can trade safely without speaking the same language. It's called the silent trade1. Herodotus2 describes a similar practice between Carthage and West Africa, and it's been reported also in Siberia, Lapland, Timor, Sumatra, India, Sri Lanka, and New Guinea.
Why didn't the Malians simply take the salt? Presumably because trade was more valuable to them in the long run. I wonder how such a custom gets started in the first place, though.
We represent a pile of salt and gold as a string (str) in which each hash (#) represents one amount of salt and each digit an amount of gold corresponding in size to the digit: the digit 3 for example represents three amounts of gold. All other characters in the string are neither salt nor gold. Your task:
Write a function salt that takes a pile of salt and gold (str). The function must return the total amount of salt (int) in the pile.
Write a function gold that takes a pile of salt and gold (str). The function must return the total amount of gold (int) in the pile.
Write a function remove_salt that takes a pile of salt and gold (str). The function must return the pile of salt and gold (str) obtained by removing all the salt from the given pile.
Write a function remove_gold that takes a pile of salt and gold (str). The function must return the pile of salt and gold (str) obtained by removing all the gold from the given pile.
Write a function trade that takes a pile of salt and gold (str). The function must return the pile of salt and gold (str) obtained by either removing all the gold or all the salt from the given pile:
the gold is removed if there is at least as much gold as salt in the given pile
the salt is removed if there is more salt than gold in the given pile
>>> salt('gj1h##hg3ur#zt#zhg#e#2h##jgs#kjl')
9
>>> salt('gj2h##hg7ur#zt#zhg#e#5h##jgs#kjl')
9
>>> gold('gj1h##hg3ur#zt#zhg#e#2h##jgs#kjl')
6
>>> gold('gj2h##hg7ur#zt#zhg#e#5h##jgs#kjl')
14
>>> remove_salt('gj1h##hg3ur#zt#zhg#e#2h##jgs#kjl')
'gj1hhg3urztzhge2hjgskjl'
>>> remove_salt('gj2h##hg7ur#zt#zhg#e#5h##jgs#kjl')
'gj2hhg7urztzhge5hjgskjl'
>>> remove_gold('gj1h##hg3ur#zt#zhg#e#2h##jgs#kjl')
'gjh##hgur#zt#zhg#e#h##jgs#kjl'
>>> remove_gold('gj2h##hg7ur#zt#zhg#e#5h##jgs#kjl')
'gjh##hgur#zt#zhg#e#h##jgs#kjl'
>>> trade('gj1h##hg3ur#zt#zhg#e#2h##jgs#kjl')
'gj1hhg3urztzhge2hjgskjl'
>>> trade('gj2h##hg7ur#zt#zhg#e#5h##jgs#kjl')
'gjh##hgur#zt#zhg#e#h##jgs#kjl'