I have a text file containing a list of numbers. I want to print only the lines that contain a number, the digits of which are consecutive digits.
The input list:
123
321
567
765
124
568
The output should be:
123
321
567
765
consecutive = Total[Abs[Differences[IntegerDigits@FromDigits@#]] - 1] &;
string = "123 321 567 765 124 568";
Pick[#, consecutive /@ #, 0]& @ StringSplit[string]
{"123", "321", "567", "765"}
string = "5 1234 56 58 123 321 567 765 124 568";
Cases[
ToExpression @ Characters @ StringSplit[string],
a : {__} /; Union @ Abs @ Differences @ a == {1} :> StringRiffle[a, ""]]
{"1234", "56", "123", "321", "567", "765"}
string = "123 321 567 765 124 568";
Using StringSplit
and Select
:
sp = StringSplit[string, " "];
consecutive = Abs[Times @@Differences[IntegerDigits[ToExpression@#]]] == 1 &;
Select[sp, consecutive@# &]
{"123", "321", "567", "765"}
Or using StringSplit
and DeleteCases
:
DeleteCases[sp, s_ /; ! consecutive[s]]
{"123", "321", "567", "765"}
Pick
andCases
could do the job? $\endgroup$