Skip to content
View in the app

A better way to browse. Learn more.

Opusmodus

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Enhancing the permute function

Featured Replies

Your implementation of permute is very nice. I looked at a few implementations on the net, but they do not handle duplicate elements correctly, whereas your implementation seems correct.

 

I would like to propose two enhancements to this function:

1) Sometimes, I might not require all permutations of a list, but only some N, where N < the maximum possible size. For example, with 4 distinct elements, 24 is the max possible, but I might just require 6. To handle this case, we can have a keyword argument to limit the size.

 

(permute '( 1 2 3 4) :limit 6) => Returns only 6 elements, not 24. Order is not guaranteed.

 

2) This is a bit tricky. Instead of restricting the number of elements explicitly as in (1), I might want to filter the elements based on some constraint. Here is a scenario:

 

(constrained-permute '(1 2 3 4) #'(lambda (e) (if (< (first e) (second e)) e nil)))

 

This will generate only those sequences where the first element of the list is less than the second element (just a hypothetical example).

 

Here is one way to implement this:

(defun constrained-permute (alist constraint)
  (filter-remove nil (mapcar constraint (permute alist))))

 

The problem with this is it is costly. It filters after generation. It will be better to apply the filter as part of the generation itself. To support this, we can add another keyword argument to permute:

 

(permute '(1 2 3 4) :filter #'(lambda (e) (if (< (first e) (second e)) e nil)))

 

Of course, we can also specify the limit if we want:

 

(permute '(1 2 3 4) :limit 6 :filter #'(lambda (e) (if (< (first e) (second e)) e nil)))

 

Do you think this makes sense?

 

Regards,

Rangarajan

Create an account or sign in to comment


Copyright © 2014-2026 Opusmodus™ Ltd. All rights reserved.
Product features, specifications, system requirements and availability are subject to change without notice.
Opusmodus, the Opusmodus logo, and other Opusmodus trademarks are either registered trademarks or trademarks of Opusmodus Ltd.
All other trademarks contained herein are the property of their respective owners.

Powered by Invision Community

Important Information

Terms of Use Privacy Policy

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.