Skip to content

Instantly share code, notes, and snippets.

@arkenidar
Created May 5, 2025 21:17
Show Gist options
  • Select an option

  • Save arkenidar/b16e9d0574977a6a233aa8600b56d654 to your computer and use it in GitHub Desktop.

Select an option

Save arkenidar/b16e9d0574977a6a233aa8600b56d654 to your computer and use it in GitHub Desktop.
[bag] lang-tech : language techniques

bag

Python bag

In Python, there isn't a built-in "bag" data type like there is in some other programming languages. However, you can achieve similar functionality using the collections.Counter class, which is part of the collections module. A Counter is essentially a dictionary for counting hashable objects. Here's a brief overview of how you can use it:

Example 1: Basic Usage

from collections import Counter

# Create a Counter object
bag = Counter()

# Add elements to the bag
bag.update(['apple', 'banana', 'apple', 'orange', 'banana', 'apple'])

# Display the contents of the bag
print(bag)
# Output: Counter({'apple': 3, 'banana': 2, 'orange': 1})

Example 2: Accessing and Modifying Counts

# Access the count of a specific element
print(bag['apple'])  # Output: 3

# Add more elements
bag.update(['apple', 'grape'])

# Remove elements
bag.subtract(['banana', 'orange'])

# Display the updated contents
print(bag)
# Output: Counter({'apple': 4, 'banana': 1, 'grape': 1, 'orange': 0})

Example 3: Iterating Over Elements

# Iterate over elements and their counts
for element, count in bag.items():
    print(f'{element}: {count}')
# Output:
# apple: 4
# banana: 1
# orange: 0
# grape: 1

Using Counter, you can effectively manage a collection of items where the order doesn't matter, and you need to keep track of the number of occurrences of each item. This makes it a suitable substitute for a "bag" data type in Python.

Lua bag

bag-impl.lua

-- file : bag-impl.lua

function BagBelongs(mybag, item)
  local amount = mybag[item]
  if amount == nil then
    amount = 0
  end
  return amount > 0 and true or false
end

function BagAdd(mybag, item)
  local amount = mybag[item]
  if amount == nil then
    amount = 0
  end
  mybag[item] = amount + 1
  return mybag
end

function BagDel(mybag, item)
  if mybag[item] == nil then
    return mybag
  end
  mybag[item] = mybag[item] - 1
  if mybag[item] == 0 then
    mybag[item] = nil
  end
  return mybag
end

function BagNew(list)
  local mybag = {}
  for _, item in pairs(list) do
    mybag = BagAdd(mybag, item)
  end
  return mybag
end

function BagList(mybag)
  for item, amount in pairs(mybag) do
    print(" item : "..item.." , ".." amount : "..amount)
  end
end

bag-use.lua

-- file : bag-use.lua

require("bag-impl")

local function TestBagFunctions()
  print("# initial bag :")
  local mybag = BagNew{ 1, 2, 3, 4, 3, 3, }
  BagList(mybag)

  print("# add 5 to bag :")
  mybag = BagAdd(mybag, 5)
  BagList(mybag)
  print("# add 5 to bag :")
  mybag = BagAdd(mybag, 5)
  BagList(mybag)

  print("# remove 3 from bag :")
  mybag = BagDel(mybag, 3)
  BagList(mybag)
  print("# remove 3 from bag :")
  mybag = BagDel(mybag, 3)
  BagList(mybag)
  print("# remove 3 from bag :")
  mybag = BagDel(mybag, 3)
  BagList(mybag)
  print("# remove 3 from bag :")
  mybag = BagDel(mybag, 3)
  BagList(mybag)

  print("# belongs to bag or not :")
  print(BagBelongs(mybag, 1) and "1 belongs" or "1 does not belong")
  print(BagBelongs(mybag, 6) and "6 belongs" or "6 does not belong")
end

TestBagFunctions()

output ( running bag-use.lua )

# initial bag :
 item : 1 ,  amount : 1
 item : 2 ,  amount : 1
 item : 3 ,  amount : 3
 item : 4 ,  amount : 1
# add 5 to bag :
 item : 1 ,  amount : 1
 item : 2 ,  amount : 1
 item : 3 ,  amount : 3
 item : 4 ,  amount : 1
 item : 5 ,  amount : 1
# add 5 to bag :
 item : 1 ,  amount : 1
 item : 2 ,  amount : 1
 item : 3 ,  amount : 3
 item : 4 ,  amount : 1
 item : 5 ,  amount : 2
# remove 3 from bag :
 item : 1 ,  amount : 1
 item : 2 ,  amount : 1
 item : 3 ,  amount : 2
 item : 4 ,  amount : 1
 item : 5 ,  amount : 2
# remove 3 from bag :
 item : 1 ,  amount : 1
 item : 2 ,  amount : 1
 item : 3 ,  amount : 1
 item : 4 ,  amount : 1
 item : 5 ,  amount : 2
# remove 3 from bag :
 item : 1 ,  amount : 1
 item : 2 ,  amount : 1
 item : 4 ,  amount : 1
 item : 5 ,  amount : 2
# remove 3 from bag :
 item : 1 ,  amount : 1
 item : 2 ,  amount : 1
 item : 4 ,  amount : 1
 item : 5 ,  amount : 2
# belongs to bag or not :
1 belongs
6 does not belong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment