The letters of the word:
S N O W M A N
are re-arranged to form a dictionary where :
1.Only 7 lettered words are included .
2.Words are arranged alphabetically.
Then, find the sum of the digits of the position of S N O W M A N in the dictionary.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Behold the power of Stephen Wolfram
Position[#,"SNOWMAN"]&@SortBy[#,First]/@(StringJoin/@Permutations@Characters["SNOWMAN"])
Anyone notice how the answer is 2013 :P?
Also, notice the SA
Log in to reply
Can you tell me what's so special about SA?
Log in to reply
sa
Log in to reply
@Ryan Soedjak – sa
Log in to reply
@Daniel Liu – sa
Log in to reply
@Ryan Soedjak – SA ??
@Ryan Soedjak – sa
sa
Log in to reply
@Daniel Liu – sa
@Daniel Liu – SA ??
TOT Senior A-level Paper. :)
I got this wrong, because obviously 2 + 0 + 1 + 3 = 5 . I used my other two tries because I misunderstood the question. At first I thought any letter could be in the dictionary, then I thought words like "AAAAAAA" are okay since "A" is in "SNOWMAN". The question could be worded better.
Anyway, nice job working 2013 in.
Extremely explanatory :D
Log in to reply
Very nice work getting "2013" in. Was that on purpose or a pleasant coincidence?
Did the same!!
Can be done rather easily in python
from itertools import permutations as perm
print sorted(set(perm([i for i in 'SNOWMAN']))).index(tuple((i for i in 'SNOWMAN')))+1
I also used the itertools module in python
Here is an even shorter version of your code.
from itertools import permutations as perm
print sorted(set(perm('snowman'))).index(tuple('snowman'))+1
Log in to reply
Cool..Didn't know the tuple function accepted str's
JAVA CODE:
public class Permutations {
static int count = 0;
static List list = new ArrayList();
static LinkedHashSet lhs = new LinkedHashSet();
public static void perm(String s) {
perm("", s);
}
private static void perm(String prefix, String s) {
int N = s.length();
if (N == 0) {
lhs.add(prefix);
} else {
for (int i = 0; i < N; i++) {
perm(prefix + s.charAt(i), s.substring(0, i) + s.substring(i + 1, N));
}
}
}
private static void swap(char[] a, int i, int j) {
char c;
c = a[i];
a[i] = a[j];
a[j] = c;
}
public static void main(String[] args) {
String str = "snowman";
char[] ca = str.toCharArray();
Arrays.sort(ca);
str = new String(ca);
perm(str);
list = Arrays.asList(lhs.toArray());
int count = list.indexOf("snowman");
System.out.println(count);
Integer pr = (Integer) count+1;
String string = pr.toString();
char[] arr = string.toCharArray();
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += (arr[i] - 48);
}
System.out.println(sum);
}
}
In Mathematica:
Position[Sort[Permutations[{s, n, o, w, m, a, n}]], {s, n, o, w, m, a, n}]
2013
Probably the best way to do this is combinatorially, but I was too lazy to go over all the options and sum them up, so I wrote the following piece of code.
def generateWords(letters):
if len(letters) == 1:
return [letters]
result = []
for i, letter in enumerate(letters):
result += [letter + subword for subword in generateWords(letters[:i] + letters[i + 1:])]
return result
words = sorted(set(generateWords('SNOWMAN')))
print words.index('SNOWMAN') + 1
generateWords(letters) is a recursive function that generate all the possible permutations of the given string. I call it with the string 'SNOWMAN', and as there are to 'N's, there could be duplicates, so I wrap the result with set() to remove duplicates.
Finally, I sort the dictionary of words, and find the index of 'SNOWMAN', which is 2012 zero-based, so 2013 1-based. 2 + 0 + 1 + 3 = 6 .
Python Solution:
import itertools
a = list(itertools.permutations('SNOWMAN'))
b = [''.join(i) for i in a]
b.sort()
c = []
for i in b:
if i not in c:
c.append(i)
print c.index('SNOWMAN') + 1
This can be solved more cleverly, but since our alphabet size is small enough, we can simply generate all the permutations and find the position of the word "SNOWMAN" there. Here's a fairly short C++ implementation:
string start = "AMNNOSW";
vector<string> words;
int main()
{
do
{
words.push_back(start);
} while (next_permutation(start.begin(), start.end()));
int ret = 1;
for (;words[ret-1] != "SNOWMAN";ret++);
printf("%d\n",ret);
}
Calling this code returns 2013, and hence the sum of digits is 6.
Naive Haskell
import Data.List
import Data.Char
import Data.Maybe
main = print $ sum $ map digitToInt $ show $ (1+) $ fromJust $ elemIndex "snowman" $ nub $ sort $ permutations "snowman"
But obviously this is asymptotically suboptimal. To provide some motivation to get a faster and more general solution, and put the coincidence of 2013 in context, let's find all words that are 2013rd in their respective dictionaries. The obvious way to speed things up is not to list all permutations that start with prefixes we don't want, but just count them with factorials. Several other solutions have demonstrated the process, so here's code:
import Data.Char
import Data.List
import Data.Maybe
import Control.Applicative
factorial n = product [1..n]
-- Given a list of frequencies of objects, count permutations of said objects
groupPerms fs = factorial (sum fs) `quot` product (map factorial fs)
-- Splice out one copy of an element
spliceOutFrom [] _ = error "spliceOutFrom failed"
spliceOutFrom (x:xs) t
| x == t = xs
| otherwise = x : spliceOutFrom xs t
-- Given a word, count how many permutations it has
lexCount ss = groupPerms $ map length $ group $ sort ss
-- Given a word, figure out its (0-based) index in a lexicographical dictionary of its permutations
lexIndex [] = 0
lexIndex [x] = 0
lexIndex (x:xs) = (sum $ map (lexCount . spliceOutFrom (x:xs)) $ filter (< x) $ nub $ sort xs) + lexIndex xs
main = putStrLn =<< unlines . filter ((== 2012) . lexIndex) . words <$> getContents
Feeding this the SOWPODS lexicon results in the list:
This being out of a lexicon with 32909 seven-letter words and 40161 eight-letter words, it's not a trivial coincidence. (But then, "SNOWING" would work for next year's Christmas too.)
Motivation : I first find the total number of ways to arrange the word, which is 2 5 2 0 . Because the words are written in alphabetical order, and next to W , the second last alphabet is S . So by working backwards, I counted the number of words that begins with W , call it x . Now we know that somewhere between 2 5 2 0 − x . Now, we have correctly placed the letter S in the first position. And because we're working backwards, we consider the letter W as the second letter and apply the same logic, and so on til we get the word S N O W M A N
We note the total number of ways total arrange S N O W M A N is 2 ! 7 ! = 2 5 2 0
We know that W is the last in the alphabets (in the list). So if the position starts with W , then the last 2 ! ( 7 − 1 ) ! = 3 6 0 words begins with W .
"Counting backwards", for words starting with S W there is a total 2 ! 5 ! = 6 0 words.
And for words starting with S O there is a total 2 ! 5 ! = 6 0 words.
Again, for words starting with S N W O , there is a total of 3 ! = 6 words.
2 5 2 0 − 3 6 0 − 6 0 − 6 0 − 6 = 2 0 3 4
Thus
The word S N O W N M A is the 2 0 3 4 th position
The word S N O W N A M is the 2 0 3 3 rd position
The word S N O W M N A is the 2 0 3 2 nd position
The word S N O W M A N is the 2 0 3 1 st position
Hence, the answer is 2 + 0 + 3 + 1 = 6
IGNORE THIS ANSWER. I'VE MADE AN ERROR!
Log in to reply
You forgot the double counting of N silly!
Log in to reply
Yeah, Bhargav Das' answer is simpler. Thanks for pointing out my mistake.
Log in to reply
@Pi Han Goh – No problem. =D
Log in to reply
@Pi Han Goh – Um... Are you talking to yourself?
Log in to reply
@Daniel Liu – no, the other one deleted his post :P
@Daniel Liu – wkwkwkw
Problem Loading...
Note Loading...
Set Loading...
First, we need to find the no. of words starting with A and there are 2 6 ! (Since,there are 2 N's )
Then,no. of words starting with M of which there are 2 6 !
Then,no. of words starting with N of which there are 6 !
Then,no. of words starting with O of which there are 2 6 !
Then,no. of words starting with SA of which there are 2 5 !
Then,no. of words starting with SM of which there are 2 5 !
Then,no. of words starting with SNA of which there are 4 !
Then,no. of words starting with SNM of which there are 4 !
Then,no. of words starting with SNN of which there are 4 !
Then,no. of words starting with SNOA of which there are 3 !
Then,no. of words starting with SNOM of which there are 3 !
Then,no. of words starting with SNON of which there are 3 !
We get a total of 2 0 1 0 words so far.
Now,we have to count words starting with SNOW till we get our desired word SNOWMAN .
We see first word starting with SNOW is SNOWAMN ,then SNOWANM and then SNOWMAN or that our desired word is the 2 0 1 3 t h word in the dictionary. Hence, our answer is 2 + 0 + 1 + 3 = 6 .