All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Algorithms
Lexicographic Listing (Posted on 2022-05-29) Difficulty: 3 of 5
N is a positive integer divisible by 7, and N does NOT contain the digit 9.

Devise an algorithm to lexicographically list all valid values of N ≤ 2022.

No Solution Yet Submitted by K Sengupta    
Rating: 4.0000 (1 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
Solution solution | Comment 1 of 3
"Lexicographically" might mean merely to do the sort with leftmost digit primary regardless of whether that's the tens position or the thousands position, etc., so that for example 1400 precedes 805 (by many positions). Or, it implies we're using words rather than digits.

The key part is that the numbers have to be converted to strings for the sort to proceed lexicographically rather than numerically, regardless of whether you let the programming language do the sort behind the scenes as done here, or whether you write your own sort using comparison operators. Writing to a file and then sorting the file would work also, if the numbers are written left justified with no leading zeros.

So the program does both, first digits, then words:


clc,clearvars
 
a=string(7:7:2022);
b=a(~contains(a,'9'));
for i=1:length(b)
  wordList(i)=words(str2double(b(i)));
end
c=sort(b);
wordList=sort(wordList);
for i=1:length(c)
  fprintf('%-5d \n',str2double(c(i)))
end
for i=1:length(wordList)
  fprintf('%-5s \n', wordList(i))
end

function w=words(n)
  major=["" " thousand" " million" " trillion"];
  w=""; ns=char(string(n));
  for i=0:3
    ns=['000',ns];
    piece=ns(end-2-3*i:end-3*i);
    if length(piece)>0
      piecePlus=['000' piece];
      piece=(piecePlus(end-2:end));
      if isequal(piece,'000')
        continue
      end
      part=procPiece(piece);
      w=strcat(part,major(i+1),w);
    end
  end  
  
  function p=procPiece(s)
    unitsTeens=["","one","two","three","four","five", ...
      "six","seven","eight","nine","ten", ...
      "eleven","twelve","thirteen","fourteen", ...
      "fifteen","sixteen","seventeen", ...
      "eighteen","nineteen"];
    tens=["","twenty","thirty","forty","fifty", ...
      "sixty","seventy","eighty","ninety"];
    a="";
    if s(1)>'0'
      a=strcat(a," ", unitsTeens(str2num(s(1))+1) ," hundred");
    end
    lastPart=str2double(s(2:3));
    if lastPart<20
      a=strcat(a," ",unitsTeens(lastPart+1));
    else
      a=strcat(a," ",tens(str2double(s(2)))," ",unitsTeens(str2double(s(3))+1));
    end
    p=a;
  end

end

1001  
1008  
1015  
1022  
1036  
1043  
105   
1050  
1057  
1064  
1071  
1078  
1085  
1106  
1113  
112   
1120  
1127  
1134  
1141  
1148  
1155  
1162  
1176  
1183  
1204  
1211  
1218  
1225  
1232  
1246  
1253  
126   
1260  
1267  
1274  
1281  
1288  
1302  
1316  
1323  
133   
1330  
1337  
1344  
1351  
1358  
1365  
1372  
1386  
14    
140   
1400  
1407  
1414  
1421  
1428  
1435  
1442  
1456  
1463  
147   
1470  
1477  
1484  
1505  
1512  
1526  
1533  
154   
1540  
1547  
1554  
1561  
1568  
1575  
1582  
1603  
161   
1610  
1617  
1624  
1631  
1638  
1645  
1652  
1666  
1673  
168   
1680  
1687  
1701  
1708  
1715  
1722  
1736  
1743  
175   
1750  
1757  
1764  
1771  
1778  
1785  
1806  
1813  
182   
1820  
1827  
1834  
1841  
1848  
1855  
1862  
1876  
1883  
2002  
2016  
203   
21    
210   
217   
224   
231   
238   
245   
252   
266   
273   
28    
280   
287   
301   
308   
315   
322   
336   
343   
35    
350   
357   
364   
371   
378   
385   
406   
413   
42    
420   
427   
434   
441   
448   
455   
462   
476   
483   
504   
511   
518   
525   
532   
546   
553   
56    
560   
567   
574   
581   
588   
602   
616   
623   
63    
630   
637   
644   
651   
658   
665   
672   
686   
7     
70    
700   
707   
714   
721   
728   
735   
742   
756   
763   
77    
770   
777   
784   
805   
812   
826   
833   
84    
840   
847   
854   
861   
868   
875   
882   
 eight hundred eighty two 
 eight hundred fifty four 
 eight hundred five 
 eight hundred forty  
 eight hundred forty seven 
 eight hundred seventy five 
 eight hundred sixty eight 
 eight hundred sixty one 
 eight hundred thirty three 
 eight hundred twelve 
 eight hundred twenty six 
 eighty four 
 fifty six 
 five hundred eighteen 
 five hundred eighty eight 
 five hundred eighty one 
 five hundred eleven 
 five hundred fifty three 
 five hundred forty six 
 five hundred four 
 five hundred seventy four 
 five hundred sixty  
 five hundred sixty seven 
 five hundred thirty two 
 five hundred twenty five 
 forty two 
 four hundred eighty three 
 four hundred fifty five 
 four hundred forty eight 
 four hundred forty one 
 four hundred seventy six 
 four hundred six 
 four hundred sixty two 
 four hundred thirteen 
 four hundred thirty four 
 four hundred twenty  
 four hundred twenty seven 
 fourteen 
 one hundred eighty two 
 one hundred fifty four 
 one hundred five 
 one hundred forty  
 one hundred forty seven 
 one hundred seventy five 
 one hundred sixty eight 
 one hundred sixty one 
 one hundred thirty three 
 one hundred twelve 
 one hundred twenty six 
 one thousand eight 
 one thousand eight hundred eighty three 
 one thousand eight hundred fifty five 
 one thousand eight hundred forty eight 
 one thousand eight hundred forty one 
 one thousand eight hundred seventy six 
 one thousand eight hundred six 
 one thousand eight hundred sixty two 
 one thousand eight hundred thirteen 
 one thousand eight hundred thirty four 
 one thousand eight hundred twenty  
 one thousand eight hundred twenty seven 
 one thousand eighty five 
 one thousand fifteen 
 one thousand fifty  
 one thousand fifty seven 
 one thousand five hundred eighty two 
 one thousand five hundred fifty four 
 one thousand five hundred five 
 one thousand five hundred forty  
 one thousand five hundred forty seven 
 one thousand five hundred seventy five 
 one thousand five hundred sixty eight 
 one thousand five hundred sixty one 
 one thousand five hundred thirty three 
 one thousand five hundred twelve 
 one thousand five hundred twenty six 
 one thousand forty three 
 one thousand four hundred  
 one thousand four hundred eighty four 
 one thousand four hundred fifty six 
 one thousand four hundred forty two 
 one thousand four hundred fourteen 
 one thousand four hundred seven 
 one thousand four hundred seventy  
 one thousand four hundred seventy seven 
 one thousand four hundred sixty three 
 one thousand four hundred thirty five 
 one thousand four hundred twenty eight 
 one thousand four hundred twenty one 
 one thousand one 
 one thousand one hundred eighty three 
 one thousand one hundred fifty five 
 one thousand one hundred forty eight 
 one thousand one hundred forty one 
 one thousand one hundred seventy six 
 one thousand one hundred six 
 one thousand one hundred sixty two 
 one thousand one hundred thirteen 
 one thousand one hundred thirty four 
 one thousand one hundred twenty  
 one thousand one hundred twenty seven 
 one thousand seven hundred eight 
 one thousand seven hundred eighty five 
 one thousand seven hundred fifteen 
 one thousand seven hundred fifty  
 one thousand seven hundred fifty seven 
 one thousand seven hundred forty three 
 one thousand seven hundred one 
 one thousand seven hundred seventy eight 
 one thousand seven hundred seventy one 
 one thousand seven hundred sixty four 
 one thousand seven hundred thirty six 
 one thousand seven hundred twenty two 
 one thousand seventy eight 
 one thousand seventy one 
 one thousand six hundred eighty  
 one thousand six hundred eighty seven 
 one thousand six hundred fifty two 
 one thousand six hundred forty five 
 one thousand six hundred seventeen 
 one thousand six hundred seventy three 
 one thousand six hundred sixty six 
 one thousand six hundred ten 
 one thousand six hundred thirty eight 
 one thousand six hundred thirty one 
 one thousand six hundred three 
 one thousand six hundred twenty four 
 one thousand sixty four 
 one thousand thirty six 
 one thousand three hundred eighty six 
 one thousand three hundred fifty eight 
 one thousand three hundred fifty one 
 one thousand three hundred forty four 
 one thousand three hundred seventy two 
 one thousand three hundred sixteen 
 one thousand three hundred sixty five 
 one thousand three hundred thirty  
 one thousand three hundred thirty seven 
 one thousand three hundred twenty three 
 one thousand three hundred two 
 one thousand twenty two 
 one thousand two hundred eighteen 
 one thousand two hundred eighty eight 
 one thousand two hundred eighty one 
 one thousand two hundred eleven 
 one thousand two hundred fifty three 
 one thousand two hundred forty six 
 one thousand two hundred four 
 one thousand two hundred seventy four 
 one thousand two hundred sixty  
 one thousand two hundred sixty seven 
 one thousand two hundred thirty two 
 one thousand two hundred twenty five 
 seven 
 seven hundred  
 seven hundred eighty four 
 seven hundred fifty six 
 seven hundred forty two 
 seven hundred fourteen 
 seven hundred seven 
 seven hundred seventy  
 seven hundred seventy seven 
 seven hundred sixty three 
 seven hundred thirty five 
 seven hundred twenty eight 
 seven hundred twenty one 
 seventy  
 seventy seven 
 six hundred eighty six 
 six hundred fifty eight 
 six hundred fifty one 
 six hundred forty four 
 six hundred seventy two 
 six hundred sixteen 
 six hundred sixty five 
 six hundred thirty  
 six hundred thirty seven 
 six hundred twenty three 
 six hundred two 
 sixty three 
 thirty five 
 three hundred eight 
 three hundred eighty five 
 three hundred fifteen 
 three hundred fifty  
 three hundred fifty seven 
 three hundred forty three 
 three hundred one 
 three hundred seventy eight 
 three hundred seventy one 
 three hundred sixty four 
 three hundred thirty six 
 three hundred twenty two 
 twenty eight 
 twenty one 
 two hundred eighty  
 two hundred eighty seven 
 two hundred fifty two 
 two hundred forty five 
 two hundred seventeen 
 two hundred seventy three 
 two hundred sixty six 
 two hundred ten 
 two hundred thirty eight 
 two hundred thirty one 
 two hundred three 
 two hundred twenty four 
 two thousand sixteen 
 two thousand two 


  Posted by Charlie on 2022-05-29 13:34:23
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (3)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information