[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Show all permutations of N coins ... without using re

Subject: Show all permutations of N coins ... without using recursion?
From: "Costello, Roger L." <costello@xxxxxxxxx>
Date: Sat, 31 Aug 2013 11:50:12 +0000
 Show all permutations of N coins ... without using  re
Hi Folks,

I want to print out all permutations of N coins.

If N = 3 then I can find all permutations using a triply nested loop:

        <xsl:for-each select="('H','T')">
            <xsl:variable name="coin1" select="." />
            <xsl:for-each select="('H','T')">
                <xsl:variable name="coin2" select="." />
                <xsl:for-each select="('H','T')">
                    <xsl:variable name="coin3" select="." />
                    <xsl:value-of select="concat($coin1, $coin2, $coin3, '
')"/>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:for-each>

Output: HHH HHT HTH HTT THH THT TTH TTT

If N is arbitrary, then that approach won't work. So I created a recursive
function that does the job, which I show below. Now I want to eliminate the
recursion and use loops instead. How do I do that? Would you please show me
how to output all permutations of N coins, without using recursion?  /Roger

-----------------------------------------------------------------------
           Show all permutations of N coins
-----------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:map="http://www.w3.org/2005/xpath-functions/map"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:f="function"
                version="3.0">

    <xsl:template match="/">
        <xsl:value-of select="f:show-coins(4, '')" />
    </xsl:template>

    <xsl:function name="f:show-coins">
        <xsl:param name="N" as="xs:integer" />
        <xsl:param name="coins" as="xs:string*" />

        <xsl:choose>
            <xsl:when test="$N eq 0">
                <xsl:value-of select="concat($coins, '  ')" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="('H','T')">
                    <xsl:value-of select="f:show-coins($N - 1, concat($coins,
.))" />
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:function>

</xsl:stylesheet>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.