Created
April 28, 2021 06:23
-
-
Save anushshukla/3bac10fd2336e196f9772be292330739 to your computer and use it in GitHub Desktop.
Python Quiz -> Fetch Highest Buyer List Store Wise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| def getCleanEntry(line): | |
| entry = line.strip('\n').split(',') | |
| return entry | |
| def getPurchaseDetails(entry): | |
| storeName = entry[1].strip() | |
| customerName = entry[0].strip() | |
| amount = int(entry[4].strip().replace("Rs ", "")) | |
| return { | |
| "storeName": storeName, | |
| "customerName": customerName, | |
| "amount": amount | |
| } | |
| def getBasicPurchaseDetails(purchaseDetails): | |
| return { | |
| "customerName": purchaseDetails['customerName'], | |
| "amount": purchaseDetails['amount'] | |
| } | |
| def appendIfNotExists(array, item): | |
| if item not in array: | |
| array.append(item) | |
| return array | |
| def run(): | |
| lines = [] | |
| storeHash = {} | |
| customerList = [] | |
| for line in sys.stdin: | |
| purchaseDetails = getPurchaseDetails(getCleanEntry(line)) | |
| storeName, customerName, amount = purchaseDetails['storeName'], purchaseDetails['customerName'], purchaseDetails['amount'] | |
| currentBasicPurchaseDetails = getBasicPurchaseDetails(purchaseDetails) | |
| if storeName not in storeHash: | |
| storeHash[storeName] = [] | |
| storeHash[storeName].append(currentBasicPurchaseDetails) | |
| customerList = appendIfNotExists(customerList, customerName) | |
| continue | |
| lastHighestPurchase = storeHash[storeName][0] | |
| lastPurchaseAmount = lastHighestPurchase['amount'] | |
| if amount > lastPurchaseAmount: | |
| storeHash[storeName] = [] | |
| storeHash[storeName].append(currentBasicPurchaseDetails) | |
| customerList = appendIfNotExists(customerList, customerName) | |
| for highestPurchase in storeHash[storeName]: | |
| customerList.remove(highestPurchase['customerName']) | |
| continue | |
| if amount == lastPurchaseAmount: | |
| customerList = appendIfNotExists(customerList, customerName) | |
| storeHash[storeName].append(currentBasicPurchaseDetails) | |
| continue | |
| for customerName in customerList: | |
| print(customerName) | |
| if __name__ == '__main__': | |
| run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rajan Patil, Aundh, 1, Phone Cover, Rs 170, Cash | |
| Mohit Gupta, Baner, 1, Samsung Battery, Rs 900, Credit Card | |
| Rajan Patil, Aundh, 3, Samsung Battery, Rs 1000, Cash | |
| Nina Kothari, Baner, 4, Earphones, Rs 500, Credit Card | |
| T Sunitha, Shivajinagar, 5, Earphones, Rs 550, Credit Card | |
| Rohan Gade, Aundh, 10, Motorola Battery, Rs 1000, Credit Card | |
| Rajan Patil, Shivajinagar, 21, Earphones, Rs 550, Credit Card | |
| Rajan Patil, Aundh, 22, USB Cable, Rs 150, UPI | |
| Meena Kothari, Baner, 23, USB Cable, Rs 100, Cash | |
| Nina Kothari, Baner, 24, USB Cable, Rs 200, UPI | |
| Mohit Gupta, Baner, 25, USB Cable, Rs 150, UPI |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment